upd readme, renamed old files, result.go is new
4 files changed
tree: e692002d3762d2de5df1013edc2e701715e42866
  1. json_schema_test_suite/
  2. .travis.yml
  3. getJson.go
  4. internalLog.go
  5. jsonContext.go
  6. LICENSE-2.0.txt
  7. locales.go
  8. README.md
  9. result.go
  10. schema.go
  11. schema_test.go
  12. schemaPool.go
  13. schemaReferencePool.go
  14. schemaType.go
  15. subSchema.go
  16. types.go
  17. utils.go
  18. utils_test.go
  19. validation.go
README.md

Build Status

gojsonschema

Description

An implementation of JSON Schema, based on IETF's draft v4 - Go language

Usage

Quick example


package main import ( "fmt" gjs "github.com/xeipuuv/gojsonschema" ) func main() { // Loads a schema remotely schema, err := gjs.NewSchema("http://host/schema.json") if err != nil { panic(err.Error()) // could not read from HTTP for example } // Loads the JSON to validate from a local file document, err := gjs.GetFile("/home/me/data.json") if err != nil { panic(err.Error()) // could be file not found on your hard drive } // Try to validate the JSON against the schema result, err := schema.Validate(document) if err != nil { panic(err.Error()) // the document is not valid JSON } // 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) } } }

Loading a schema

Schemas can be loaded remotely from a HTTP Url:

    schemaDocument, err := gjs.NewSchema("http://myhost/schema.json")

From a local file, using the file URI scheme:

	schemaDocument, err := gjs.NewSchema("file:///home/me/schema.json")

You may also load the schema from within your code, using a map[string]interface{} variable or a JSON string.

Note that schemas loaded from non-HTTP are subject to limitations, they need to be standalone schemas; Which means references to local files and/or remote files within these schemas will not work.

	schemaMap := map[string]interface{}{
		"type": "string"}

	schema, err := gojsonschema.NewSchema(schemaMap)

	// or using a string
	// schema, err := gojsonschema.NewSchema("{\"type\": \"string\"}")

Loading a JSON

The library virtually accepts any JSON since it uses reflection to validate against the schema.

You may use and combine go types like

  • string (JSON string)
  • bool (JSON boolean)
  • float64 (JSON number)
  • nil (JSON null)
  • slice (JSON array)
  • map[string]interface{} (JSON object)

You can declare your Json from within your code, using a map / interface{}:

	jsonDocument := map[string]interface{}{
		"name": "john"}

A string:

	jsonDocument := "{\"name\": \"john\"}"

Helper functions are also available to load from a Http URL:

    jsonDocument, err := gojsonschema.GetHTTP("http://host/data.json")

Or a local file:

	jsonDocument, err := gojsonschema.GetFile("/home/me/data.json")

Validation

Once the schema and the JSON to validate are loaded, validation phase becomes easy:

	result, err := 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)
	}

Dependencies

https://github.com/xeipuuv/gojsonpointer

https://github.com/xeipuuv/gojsonreference

https://github.com/stretchr/testify/assert

Uses

gojsonschema uses the following test suite :

https://github.com/json-schema/JSON-Schema-Test-Suite

References

###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