golang implementation of https://json-schema.org
Clone or download
dustmop feat(jsonschema): Field to tell if RootSchema is an array or object.
Merge pull request #30 from qri-io/top-is-array
Latest commit d0d3b10 Jun 7, 2018
Permalink
Failed to load latest commit information.
.circleci chore(ci): quick fixes to ci pipeline Mar 8, 2018
.github docs: added developer and contributing docs along with code of conduct Jan 17, 2018
testdata feat(json.Marshaler): marshal schemas back to json properly. Jan 17, 2018
.gitignore feat: initial commit Jan 12, 2018
DEVELOPERS.md docs: removed reference to standardjs Jan 17, 2018
LICENSE feat: initial support for local references Jan 15, 2018
README.md docs(readme): remove new package alert Apr 10, 2018
code_of_conduct.md docs: added developer and contributing docs along with code of conduct Jan 17, 2018
codecov.yml chore(ci): quick fixes to ci pipeline Mar 8, 2018
keywords.go feat(jsonschema): Cleanup mistakes, test for unknown schema type. Jun 7, 2018
keywords_arrays.go feat(ValError): overhaul and upgrade error collection & reporting Mar 9, 2018
keywords_booleans.go feat(ValError): overhaul and upgrade error collection & reporting Mar 9, 2018
keywords_conditionals.go feat(ValError): overhaul and upgrade error collection & reporting Mar 9, 2018
keywords_format.go feat(ValError): overhaul and upgrade error collection & reporting Mar 9, 2018
keywords_numeric.go feat(ValError): overhaul and upgrade error collection & reporting Mar 9, 2018
keywords_objects.go feat(ValError): overhaul and upgrade error collection & reporting Mar 9, 2018
keywords_strings.go feat(ValError): overhaul and upgrade error collection & reporting Mar 9, 2018
schema.go feat(jsonschema): Cleanup mistakes, test for unknown schema type. Jun 7, 2018
schema_test.go feat(jsonschema): Cleanup mistakes, test for unknown schema type. Jun 7, 2018
traversal.go feat(refs): first signs of life on refs working properly Jan 16, 2018
traversal_test.go feat(ValError): overhaul and upgrade error collection & reporting Mar 9, 2018
validate.go feat(ValError): overhaul and upgrade error collection & reporting Mar 9, 2018
validate_test.go feat(ValError): overhaul and upgrade error collection & reporting Mar 9, 2018

README.md

jsonschema

Qri GoDoc License Codecov CI Go Report Card

golang implementation of the JSON Schema Specification, which lets you write JSON that validates some other json. Rad.

Package Features

  • Encode schemas back to JSON
  • Supply Your own Custom Validators
  • Uses Standard Go idioms

Getting Involved

We would love involvement from more people! If you notice any errors or would like to submit changes, please see our Contributing Guidelines.

Developing

We've set up a separate document for developer guidelines!

Basic Usage

Here's a quick example pulled from the godoc:

package main

import (
	"encoding/json"
	"fmt"

	"github.com/qri-io/jsonschema"
)

func main() {
	var schemaData = []byte(`{
      "title": "Person",
      "type": "object",
      "properties": {
          "firstName": {
              "type": "string"
          },
          "lastName": {
              "type": "string"
          },
          "age": {
              "description": "Age in years",
              "type": "integer",
              "minimum": 0
          },
          "friends": {
            "type" : "array",
            "items" : { "title" : "REFERENCE", "$ref" : "#" }
          }
      },
      "required": ["firstName", "lastName"]
    }`)

	rs := &jsonschema.RootSchema{}
	if err := json.Unmarshal(schemaData, rs); err != nil {
		panic("unmarshal schema: " + err.Error())
	}

	var valid = []byte(`{
    "firstName" : "George",
    "lastName" : "Michael"
    }`)

	if errors := rs.ValidateBytes(valid); len(errors) > 0 {
		panic(err)
	}

	var invalidPerson = []byte(`{
    "firstName" : "Prince"
    }`)
	if errors := rs.ValidateBytes(invalidPerson); len(errors) > 0 {
  	fmt.Println(errs[0].Error())
  }

	var invalidFriend = []byte(`{
    "firstName" : "Jay",
    "lastName" : "Z",
    "friends" : [{
      "firstName" : "Nas"
      }]
    }`)
	if errors := rs.ValidateBytes(invalidFriend); len(errors) > 0 {
  	fmt.Println(errors[0].Error())
  }
}

Custom Validators

The godoc gives an example of how to supply your own validators to extend the standard keywords supported by the spec.

It involves two steps that should happen before allocating any RootSchema instances that use the validator:

  1. create a custom type that implements the Validator interface
  2. call RegisterValidator with the keyword you'd like to detect in JSON, and a ValMaker function.
package main

import (
  "encoding/json"
  "fmt"
  "github.com/qri-io/jsonschema"
)

// your custom validator
type IsFoo bool

// newIsFoo is a jsonschama.ValMaker
func newIsFoo() jsonschema.Validator {
  return new(IsFoo)
}

// Validate implements jsonschema.Validator
func (f IsFoo) Validate(data interface{}) []jsonschema.ValError {
  if str, ok := data.(string); ok {
    if str != "foo" {
      return []jsonschema.ValError{
        {Message: fmt.Sprintf("'%s' is not foo. It should be foo. plz make '%s' == foo. plz", str, str)},
      }
    }
  }
  return nil
}

func main() {
  // register a custom validator by supplying a function
  // that creates new instances of your Validator.
  jsonschema.RegisterValidator("foo", newIsFoo)

  schBytes := []byte(`{ "foo": true }`)

  // parse a schema that uses your custom keyword
  rs := new(jsonschema.RootSchema)
  if err := json.Unmarshal(schBytes, rs); err != nil {
    // Real programs handle errors.
    panic(err)
  }

  // validate some JSON
  errors := rs.ValidateBytes([]byte(`"bar"`))

  // print le error
  fmt.Println(errs[0].Error())

  // Output: 'bar' is not foo. It should be foo. plz make 'bar' == foo. plz
}