Skip to content

Commit

Permalink
chore(docs): Update README.md (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbren authored Mar 18, 2021
1 parent dbc3af1 commit 71c7745
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```

0 comments on commit 71c7745

Please sign in to comment.