Skip to content
Peter Svetlichny edited this page Aug 3, 2015 · 5 revisions

Validation for any instance type.


enum

Description

Examples

Simple enum validation

Schema

"schema": {"enum": [1, 2, 3]}

API

var schema = json.enum([1, 2, 3]);

or simply

var schema = json.enum(1, 2, 3);

type


Integer type matches integers

Schema

"schema": {"type": "integer"}

API

var schema = json.integer();

Specifying multiple types

Schema

"schema": {"type": ["integer", "string"]}

API

var schema = json.type(['integer', 'string']);

allOf


allOf with base schema

Schema

"schema": {
	"properties": {"bar": {"type": "integer"}},
	"required": ["bar"],
	"allOf" : [
		{
			"properties": {
				"foo": {"type": "string"}
			},
			"required": ["foo"]
		},
		{
			"properties": {
				"baz": {"type": "null"}
			},
			"required": ["baz"]
		}
	]
}

API

var schema = json
              .property('bar', json.integer(), true)
              .allOf([
                json.property('foo', json.string(), true),
                json.property('baz', json.null(), true)]);

anyOf


anyOf with base schema

Schema

"schema": {
	"type": "string",
	"anyOf" : [
		{
			"maxLength": 2
		},
		{
			"minLength": 4
		}
	]
}

API

var schema = json
              .string()
              .anyOf([json.maxLength(2), json.minLength(4)]);

oneOf


oneOf with base schema

Schema

"schema": {
	"type": "string",
	"oneOf" : [
		{
			"minLength": 2
		},
		{
			"maxLength": 4
		}
	]
}

API

var schema = json
              .string()
              .oneOf([json.minLength(2), json.maxLength(4)]);

not


not multiple types

Schema

"schema": {
	"not": {"type": ["integer", "boolean"]}
}

API

var schema = json
              .not(json.type(['integer', 'boolean']));

definitions


valid definition

Schema

"schema": {"$ref": "http://json-schema.org/draft-04/schema#"}

API

var schema = json.$ref('http://json-schema.org/draft-04/schema#');
Clone this wiki locally