diff --git a/README.md b/README.md index ca0f422..134f7fc 100644 --- a/README.md +++ b/README.md @@ -132,60 +132,65 @@ It involves three steps that should happen _before_ allocating any Schema instan package main import ( - "encoding/json" - "fmt" + "context" + "encoding/json" + "fmt" - "github.com/qri-io/jsonschema" + jptr "github.com/qri-io/jsonpointer" + "github.com/qri-io/jsonschema" ) // your custom validator type IsFoo bool // newIsFoo is a jsonschama.KeyMaker -func newIsFoo() Keyword { - return new(IsFoo) +func newIsFoo() jsonschema.Keyword { + return new(IsFoo) } // Validate implements jsonschema.Keyword -func (f *IsFoo) Validate(propPath string, data interface{}, errs *[]KeyError) {} +func (f *IsFoo) Validate(propPath string, data interface{}, errs *[]jsonschema.KeyError) {} // Register implements jsonschema.Keyword -func (f *IsFoo) Register(uri string, registry *SchemaRegistry) {} +func (f *IsFoo) Register(uri string, registry *jsonschema.SchemaRegistry) {} // Resolve implements jsonschema.Keyword -func (f *IsFoo) Resolve(pointer jptr.Pointer, uri string) *Schema { - return nil +func (f *IsFoo) Resolve(pointer jptr.Pointer, uri string) *jsonschema.Schema { + return nil } // ValidateKeyword implements jsonschema.Keyword -func (f *IsFoo) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) { - if str, ok := data.(string); ok { - if str != "foo" { - currentState.AddError(data, fmt.Sprintf("should be foo. plz make '%s' == foo. plz", str)) +func (f *IsFoo) ValidateKeyword(ctx context.Context, currentState *jsonschema.ValidationState, data interface{}) { + if str, ok := data.(string); ok { + if str != "foo" { + currentState.AddError(data, fmt.Sprintf("should be foo. plz make '%s' == foo. plz", str)) + } } - } } func main() { - // register a custom validator by supplying a function - // that creates new instances of your Validator. - jsonschema.RegisterKeyword("foo", newIsFoo) + // register a custom validator by supplying a function + // that creates new instances of your Validator. + jsonschema.RegisterKeyword("foo", newIsFoo) - schBytes := []byte(`{ "foo": true }`) + // If you register a custom validator, you'll need to manually register + // any other JSON Schema validators you need. + jsonschema.LoadDraft2019_09() - rs := new(Schema) - if err := json.Unmarshal(schBytes, rs); err != nil { - // Real programs handle errors. - panic(err) - } + schBytes := []byte(`{ "foo": true }`) - errs, err := rs.ValidateBytes([]byte(`"bar"`)) - if err != nil { - panic(err) - } + rs := new(jsonschema.Schema) + if err := json.Unmarshal(schBytes, rs); err != nil { + // Real programs handle errors. + panic(err) + } - fmt.Println(errs[0].Error()) - // Output: /: "bar" should be foo. plz make 'bar' == foo. plz + errs, err := rs.ValidateBytes(context.Background(), []byte(`"bar"`)) + if err != nil { + panic(err) + } + fmt.Println(errs[0].Error()) + // Output: /: "bar" should be foo. plz make 'bar' == foo. plz } ```