commit | f91f7ce16b9ecfb8b6bcee704e195bd53e5dbea5 | [log] [tgz] |
---|---|---|
author | xeipuuv <xeipuuv@gmail.com> | Tue Jun 24 16:15:02 2014 +0800 |
committer | xeipuuv <xeipuuv@gmail.com> | Tue Jun 24 16:15:02 2014 +0800 |
tree | b4c6aa04a3e94e2eff9d7d647c60dd2260f347f1 | |
parent | ffdac0eacceb85496638f730516e2492b3e4a8ef [diff] | |
parent | ca50e7c6ac662c8433236903e26e761a341ac8d3 [diff] |
Merge pull request #22 from binary132/xei-fix schemaDocument now supports maximum and minimum values of int type
An implementation of JSON Schema, based on IETF's draft v4 - Go language
One feature is missing : id(s) as scope for references
Test results : Passed 99.20% of Json Schema Test Suite ( 246 / 248 )
package main import ( "fmt" "github.com/sigu-399/gojsonschema" ) func main() { // Loads a schema remotely schemaDocument, err := gojsonschema.NewJsonSchemaDocument("http://host/schema.json") if err != nil { panic(err.Error()) } // Loads the JSON to validate from a local file jsonDocument, err := gojsonschema.GetFileJson("/home/me/data.json") if err != nil { panic(err.Error()) } // Try to validate the Json against the schema result := schemaDocument.Validate(jsonDocument) // Deal with result if result.Valid() { fmt.Printf("The document is valid\n") } else { fmt.Printf("The document is not valid. see errors :\n") // Loop through errors for _, desc := range result.Errors() { fmt.Printf("- %s\n", desc) } } }
Schemas can be loaded remotely from a Http Url:
schemaDocument, err := gojsonschema.NewJsonSchemaDocument("http://myhost/schema.json")
Or a local file, using the file URI scheme:
schemaDocument, err := gojsonschema.NewJsonSchemaDocument("file:///home/me/schema.json")
You may also load the schema from within your code, using a map[string]interface{} variable.
Note that schemas loaded that way are subject to limitations, they need to be standalone schemas; Which means references to local files and/or remote files within this schema will not work.
schemaMap := map[string]interface{}{
"type": "string"}
schemaDocument, err := gojsonschema.NewJsonSchemaDocument(schemaMap)
The library virtually accepts any Json since it uses reflection to validate against the schema.
You may use and combine go types like
You may declare your Json from within your code:
jsonDocument := map[string]interface{}{
"name": "john"}
Helper functions are also available to load from a Http URL:
jsonDocument, err := gojsonschema.GetHttpJson("http://host/data.json")
Or a local file:
jsonDocument, err := gojsonschema.GetFileJson("/home/me/data.json")
Once the schema and the Json to validate are loaded, validation phase becomes easy:
result := schemaDocument.Validate(jsonDocument)
Check the result validity with:
if result.Valid() { // Your Json is valid }
If not valid, you can loop through the error messages returned by the validation phase:
for _, desc := range result.Errors() {
fmt.Printf("Error: %s\n", desc)
}
###Website http://json-schema.org
###Schema Core http://json-schema.org/latest/json-schema-core.html
###Schema Validation http://json-schema.org/latest/json-schema-validation.html
https://github.com/sigu-399/gojsonpointer
https://github.com/sigu-399/gojsonreference
gojsonschema uses the following test suite :