@@ -132,60 +132,65 @@ It involves three steps that should happen _before_ allocating any Schema instan
132132package main
133133
134134import (
135- " encoding/json"
136- " fmt"
135+ " context"
136+ " encoding/json"
137+ " fmt"
137138
138- " github.com/qri-io/jsonschema"
139+ jptr " github.com/qri-io/jsonpointer"
140+ " github.com/qri-io/jsonschema"
139141)
140142
141143// your custom validator
142144type IsFoo bool
143145
144146// newIsFoo is a jsonschama.KeyMaker
145- func newIsFoo () Keyword {
146- return new (IsFoo)
147+ func newIsFoo () jsonschema . Keyword {
148+ return new (IsFoo)
147149}
148150
149151// Validate implements jsonschema.Keyword
150- func (f *IsFoo ) Validate (propPath string , data interface {}, errs *[]KeyError ) {}
152+ func (f *IsFoo ) Validate (propPath string , data interface {}, errs *[]jsonschema . KeyError ) {}
151153
152154// Register implements jsonschema.Keyword
153- func (f *IsFoo ) Register (uri string , registry *SchemaRegistry ) {}
155+ func (f *IsFoo ) Register (uri string , registry *jsonschema . SchemaRegistry ) {}
154156
155157// Resolve implements jsonschema.Keyword
156- func (f *IsFoo ) Resolve (pointer jptr .Pointer , uri string ) *Schema {
157- return nil
158+ func (f *IsFoo ) Resolve (pointer jptr .Pointer , uri string ) *jsonschema . Schema {
159+ return nil
158160}
159161
160162// ValidateKeyword implements jsonschema.Keyword
161- func (f *IsFoo ) ValidateKeyword (ctx context .Context , currentState *ValidationState , data interface {}) {
162- if str , ok := data.(string ); ok {
163- if str != " foo" {
164- currentState.AddError (data, fmt.Sprintf (" should be foo. plz make '%s ' == foo. plz" , str))
163+ func (f *IsFoo ) ValidateKeyword (ctx context .Context , currentState *jsonschema .ValidationState , data interface {}) {
164+ if str , ok := data.(string ); ok {
165+ if str != " foo" {
166+ currentState.AddError (data, fmt.Sprintf (" should be foo. plz make '%s ' == foo. plz" , str))
167+ }
165168 }
166- }
167169}
168170
169171func main () {
170- // register a custom validator by supplying a function
171- // that creates new instances of your Validator.
172- jsonschema.RegisterKeyword (" foo" , newIsFoo)
172+ // register a custom validator by supplying a function
173+ // that creates new instances of your Validator.
174+ jsonschema.RegisterKeyword (" foo" , newIsFoo)
173175
174- schBytes := []byte (` { "foo": true }` )
176+ // If you register a custom validator, you'll need to manually register
177+ // any other JSON Schema validators you need.
178+ jsonschema.LoadDraft2019_09 ()
175179
176- rs := new (Schema)
177- if err := json.Unmarshal (schBytes, rs); err != nil {
178- // Real programs handle errors.
179- panic (err)
180- }
180+ schBytes := []byte (` { "foo": true }` )
181181
182- errs , err := rs.ValidateBytes ([]byte (` "bar"` ))
183- if err != nil {
184- panic (err)
185- }
182+ rs := new (jsonschema.Schema )
183+ if err := json.Unmarshal (schBytes, rs); err != nil {
184+ // Real programs handle errors.
185+ panic (err)
186+ }
186187
187- fmt.Println (errs[0 ].Error ())
188- // Output: /: "bar" should be foo. plz make 'bar' == foo. plz
188+ errs , err := rs.ValidateBytes (context.Background (), []byte (` "bar"` ))
189+ if err != nil {
190+ panic (err)
191+ }
192+ fmt.Println (errs[0 ].Error ())
193+ // Output: /: "bar" should be foo. plz make 'bar' == foo. plz
189194}
190195```
191196
0 commit comments