@@ -13,7 +13,7 @@ golang implementation of the [JSON Schema Specification](http://json-schema.org/
1313* Encode schemas back to JSON
1414* Supply Your own Custom Validators
1515* Uses Standard Go idioms
16- * Fastest Go implementation of [ JSON Schema validators] ( http://json-schema.org/implementations.html#validators ) (draft2019_9 only, (old - draft 7) benchmarks are [ here] ( https://github.com/TheWildBlue/validator-benchmarks ) - thanks [ @TheWildBlue ] ( https://github.com/TheWildBlue ) !)
16+ * Fastest Go implementation of [ JSON Schema validators] ( http://json-schema.org/implementations.html#validators ) (draft2019_9 only, (old — draft 7) benchmarks are [ here] ( https://github.com/TheWildBlue/validator-benchmarks ) — thanks [ @TheWildBlue ] ( https://github.com/TheWildBlue ) !)
1717
1818### Getting Involved
1919
@@ -23,23 +23,25 @@ like to submit changes, please see our
2323
2424### Developing
2525
26- We' ve set up a separate document for [ developer guidelines] ( https://github.com/qri-io/jsonschema/blob/master/DEVELOPERS.md ) !
26+ We’ ve set up a separate document for [ developer guidelines] ( https://github.com/qri-io/jsonschema/blob/master/DEVELOPERS.md ) !
2727
2828## Basic Usage
2929
30- Here' s a quick example pulled from the [ godoc] ( https://godoc.org/github.com/qri-io/jsonschema ) :
30+ Here’ s a quick example pulled from the [ godoc] ( https://godoc.org/github.com/qri-io/jsonschema ) :
3131
3232``` go
3333package main
3434
3535import (
36+ " context"
3637 " encoding/json"
3738 " fmt"
3839
3940 " github.com/qri-io/jsonschema"
4041)
4142
4243func main () {
44+ ctx := context.Background ()
4345 var schemaData = []byte (` {
4446 "$id": "https://qri.io/schema/",
4547 "$comment" : "sample comment",
@@ -65,51 +67,55 @@ func main() {
6567 "required": ["firstName", "lastName"]
6668 }` )
6769
68- rs := &Schema{}
69- if err := json.Unmarshal (schemaData, rs); err != nil {
70- panic (" unmarshal schema: " + err.Error ())
71- }
70+ rs := &jsonschema. Schema {}
71+ if err := json.Unmarshal (schemaData, rs); err != nil {
72+ panic (" unmarshal schema: " + err.Error ())
73+ }
7274
73- var valid = []byte (` {
75+ var valid = []byte (` {
7476 "firstName" : "George",
7577 "lastName" : "Michael"
7678 }` )
77- errs , err := rs.ValidateBytes (valid)
78- if err != nil {
79- panic (err)
80- }
79+ errs , err := rs.ValidateBytes (ctx, valid)
80+ if err != nil {
81+ panic (err)
82+ }
8183
82- if len (errs) > 0 {
83- fmt.Println (errs[0 ].Error ())
84- }
84+ if len (errs) > 0 {
85+ fmt.Println (errs[0 ].Error ())
86+ }
8587
86- var invalidPerson = []byte (` {
88+ var invalidPerson = []byte (` {
8789 "firstName" : "Prince"
8890 }` )
8991
90- errs, err = rs.ValidateBytes (invalidPerson)
91- if err != nil {
92- panic (err)
93- }
94- if len (errs) > 0 {
95- fmt.Println (errs[0 ].Error ())
96- }
92+ errs, err = rs.ValidateBytes (ctx, invalidPerson)
93+ if err != nil {
94+ panic (err)
95+ }
96+ if len (errs) > 0 {
97+ fmt.Println (errs[0 ].Error ())
98+ }
9799
98- var invalidFriend = []byte (` {
100+ var invalidFriend = []byte (` {
99101 "firstName" : "Jay",
100102 "lastName" : "Z",
101103 "friends" : [{
102104 "firstName" : "Nas"
103105 }]
104106 }` )
105- errs, err = rs.ValidateBytes (invalidFriend)
106- if err != nil {
107- panic (err)
108- }
109- if len (errs) > 0 {
110- fmt.Println (errs[0 ].Error ())
111- }
107+ errs, err = rs.ValidateBytes (ctx, invalidFriend)
108+ if err != nil {
109+ panic (err)
110+ }
111+ if len (errs) > 0 {
112+ fmt.Println (errs[0 ].Error ())
113+ }
112114}
115+
116+ // Output:
117+ // /: {"firstName":"Prince... "lastName" value is required
118+ // /friends/0: {"firstName":"Nas"} "lastName" value is required
113119```
114120
115121## Custom Keywords
@@ -119,7 +125,7 @@ The [godoc](https://godoc.org/github.com/qri-io/jsonschema) gives an example of
119125It involves three steps that should happen _ before_ allocating any Schema instances that use the validator:
1201261 . create a custom type that implements the ` Keyword ` interface
1211272 . Load the appropriate draft keyword set (see ` draft2019_09_keywords.go ` )
122- 3 . call RegisterKeyword with the keyword you' d like to detect in JSON, and a ` KeyMaker ` function.
128+ 3 . call RegisterKeyword with the keyword you’ d like to detect in JSON, and a ` KeyMaker ` function.
123129
124130
125131``` go
0 commit comments