a zero dependency jsonschema implementation
$: jsonschema **/*
schema, err := jsonschema.Read("./schema.json")
if err != nil {
panic(err)
}
errs := jsonschema.Compile(schema)
if len(errs) > 0 {
panic(errs)
}
schema := jsonschema.Builder().
Object().
Properties(map[string]jsonschema.Schema{
"test": jsonschema.Builder().String().Build(),
}).
AdditionalProperties(jsonschema.Builder().Integer().Build()).
Required("test").
Build()
errs := jsonschema.Compile(schema)
if len(errs) > 0 {
panic(errs)
}
errs = jsonschema.Validate(schema, struct {
Test string `json:"test"`
Other int `json:"other"`
}{"test", 1})
if len(errs) > 0 {
panic(errs)
}
namespace := jsonschema.New()
jsonschema.AddKeyword("alphaNum", jsonschema.Keyword{ ... })
jsonschema.AddFormat("lowercase", func(input string) error {
if strings.ToLower(input) != input {
return errors.New("must be lowercase")
}
return nil
})
- date-time
- ipv4
- ipv6
- uri
- uuid
Draft | Status |
---|---|
4 | ✅ |
6 | ✅ |
7 | ✅ |
2019-09 | ⏳ |
2020-12 | ⏳ |
Name | Status |
---|---|
Custom Keywords | ✅ |
Custom Error Messages | ⏳ |
Custom Formats | ✅ |
- schema builder pattern
- cli
- struct tags
- add message keyword for custom error messages
- keywords:
- object
- unevaluatedProperties
- array
- prefixItems
- unevaluatedItems
- minContains
- maxContains
- deprecated
- readOnly
- writeOnly
- examples
- contentMediaType
- contentEncoding
- if/then/else
- object