Skip to content
Tony Pujals edited this page Aug 4, 2015 · 6 revisions

Validation for object types.

Specify a property

var schema = json.property('foo');

Generates

{
  "properties": {
    "foo": {}
  }
}

Matches any of the following

{}

{
  "foo": 1
}

{
  "foo": "hello",
  "bar": "world
}

Specify schema type explicitly and a property

var schema = json.object().property('foo');

Generates

{
  "type": "object",
  "properties": {
    "foo": {}
  }
}

Matches the same as the previous example.

Specify that any property value is allowed explicitly

var schema = json.object().property('foo', {});

Generates the same as the previous example.

Matches the same as the previous example.

Specify that additional properties are allowed explicitly

var schema = json.object()
  .property('foo', {})
  .additionalProperties(true);

Generates

{
  "type": "object",
  "properties": {
    "foo": {}
  },
  "additionalProperties": true
}

Matches the same as the previous example.

Specify that additional properties are allowed explicitly

var schema = json.object()
  .property('foo', {})
  .additionalProperties(true);

Generates

{
  "type": "object",
  "properties": {
    "foo": {}
  },
  "additionalProperties": true
}

Matches the same as the previous example.

Specify that additional properties are not allowed

var schema = json.object()
  .property('foo', {})
  .additionalProperties(false);

Generates

{
  "type": "object",
  "properties": {
    "foo": {}
  },
  "additionalProperties": false
}

Matches

{}

{
  "foo": 1
}

Specify that the property is required, and no others are allowed

var schema = json.object()
  .property('foo', {}, true)
  .additionalProperties(false);

or

var schema = json.object()
  .property('foo')
  .required('foo')
  .additionalProperties(false);

Generates

{
  "type": "object",
  "properties": {
    "foo": {}
  },
  "required": [ "foo" ],
  "additionalProperties": false
}

Matches

{
  "foo": 1
}

Specify property type explicitly

Use optional flag to specify if property is required (default is false).

var schema = json
  .property('foo', json.string(), true)
  .property('bar', json.integer());

Generates

{
  "properties": {
    "foo": { "type": "string" },
    "bar": { "type": "integer" }
  },
  "required": [ "foo" ]
}

maxProperties


maxProperties validation

Schema

"schema": {"maxProperties": 2}

API

var schema = json.maxProperties(2);

minProperties


minProperties validation

Schema

"schema": {"minProperties": 1}

API

var schema = json.minProperties(1);

required


required validation

Schema

"schema": {
	"properties": {
		"foo": {},
		"bar": {}
	},
	"required": ["foo"]
}

API

var schema = json
        .property('foo', {}, true)
        .property('bar', {});

additionalProperties


additionalProperties can exist by itself

Schema

"schema": {
	"additionalProperties": {"type": "boolean"}
}

API

var schema = json.additionalProperties(json.boolean());

properties


object properties validation

Schema

"schema": {
	"properties": {
		"foo": {"type": "integer"},
		"bar": {"type": "string"}
	}
}

API

var schema = json
          .properties({
            foo: json.integer(),
            bar: json.string()
          });

Equivalent usage:

var schema = json
              .property('foo', json.integer())
              .property('bar', json.string());

patternProperties


multiple simultaneous patternProperties are validated

Schema

"schema": {
	"patternProperties": {
		"a*": {"type": "integer"},
		"aaa*": {"maximum": 20}
	}
}

API

var schema = json
              .patternProperty('a*', json.integer())
              .patternProperty('aaa*', json.maximum(20));

dependencies


dependencies validation

Schema

"schema": {
	"dependencies": {"bar": ["foo"]}
}

API

var schema = json.dependencies({ 'bar': ['foo'] });