-
Notifications
You must be signed in to change notification settings - Fork 12
Schema validation constraints for any instance type.
Validation constraints restrict the set of JSON instances that can match a schema. This section documents the constraints that can be used with any schema for matching any instance type.
One of the constraints in this section is type
. For schemas that have a type
constraint, there are additional constraints that can be applied to further restrict matching JSON instances depending on whether the type is numeric (number
or integer
), string
, array
, or object
. See the sidebar for links to these additional constraints.
enum
is constraint that can apply to any schema to restrict the set of acceptable values.
The set of enum
values do not need to all be of the same type -- however if a type
constraint is also specified, it is important to ensure that the enum
values are consistent with the specific type
constraint.
var schema = jsb.enum( [ 'foo', 3, true ] );
or simply
var schema = jsb.enum( 'foo', 3, true );
schema.json()
generates:
{
"enum": [ 'foo', 3, true ]
}
which matches "foo"
, 3
, and true
JSON instances.
As mentioned previously, take care to ensure enum
constraint values are consistent with the type
constraint. The following generates a valid schema, but only a JSON instance with the value 3
will match.
var schema = jsb.integer().enum('foo', 3, true);
schema.json()
generates:
{
"type": "integer",
"enum": [ 'foo', 3, true ]
}
which will only match 3
, since it is the only enum value that is an integer.
Schema
"schema": { "enum": [1, 2, 3] }
API
var schema = json.enum([1, 2, 3]);
or simply
var schema = json.enum(1, 2, 3);
All valid JSON Schema types are supported:
var integerSchema = jsb.integer();
var numberSchema = jsb.number();
var booleanSchema = jsb.boolean();
var stringSchema = jsb.string();
var arraySchema = jsb.array();
var objectSchema = jsb.object();
var nullSchema = jsb.null();
Using integerSchema
from this example, integerSchema.json()
would generate the following JSON Schema document (or fragment):
{
"type": "integer"
}
This schema can be used by a validator to match any integer JSON instance (any number without a fraction or exponent part).
The type
constraint can be used to restrict JSON instances to a particular set of acceptable types. The following example demonstrates how to specify a list of types:
var schema = jsb.type( 'integer', 'string' );
The string values can be any valid JSON Schema type (boolean
, integer
, number
, string
, array
, object
, and null
).
Schema
"schema": { "type": "integer" }
API
var schema = json.integer();
Schema
"schema": {"type": ["integer", "string"]}
API
var schema = json.type(['integer', 'string']);
allOf
, anyOf
, and oneOf
contraints apply to any schema. They accept an arguments list or an array of builder objects.
allOf
is a list of one or more schemas all of which must match for a JSON instance to be valid.
var allOf = jsb.string().allOf(
jsb.enum('foo', 'bar', 'foobaz'),
jsb.minLength(4));
allOf.json()
generates the following:
{
"type": "string",
"allOf": [
{ "enum": [ "foo", "bar", "foobaz" ] },
{ "minLength": 4 }
]
}
This will only match JSON instances that are strings that have a value of "foobaz"
, since it is the only enum value that is at least 4 characters long (which satisfies both schemas).
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)]);
allOf
, anyOf
, and oneOf
contraints apply to any schema. They accept an arguments list or an array of builder objects.
anyOf
is a list of one or more schemas at least one of which must match for a JSON instance to be valid.
var anyOf = jsb.string().anyOf(
jsb.enum('foo', 'bar', 'foobaz'),
jsb.minLength(10));
anyOf.json()
generates the following:
{
"type": "string",
"anyOf": [
{ "enum": [ "foo", "bar", "foobaz" ] },
{ "minLength": 10 }
]
}
This will match JSON instances that are strings that have a value that is either "foo"
, "bar"
, "foobaz"
, or is at least 10 characters long (satisfying at least one of the schemas).
Schema
"schema": {
"type": "string",
"anyOf" : [
{
"maxLength": 2
},
{
"minLength": 4
}
]
}
API
var schema = json
.string()
.anyOf([json.maxLength(2), json.minLength(4)]);
allOf
, anyOf
, and oneOf
contraints apply to any schema. They accept an arguments list or an array of builder objects.
oneOf
is a list of one or more schemas only one of which can match for a JSON instance to be valid.
var oneOf = jsb.string().oneOf(
jsb.enum('foo', 'bar', 'foobaz'),
jsb.minLength(4));
oneOf.json()
generates the following:
{
"type": "string",
"oneOf": [
{ "enum": [ "foo", "bar", "foobaz" ] },
{ "minLength": 4 }
]
}
This will match JSON instances that are either strings that have a value that is either "foo"
, "bar"
, but not "foobaz"
(since that would also match the second rule); or have at least 4 characters (but again, not "foobaz"
since that would also match the first schema, and only one of the schemas can be satisfied).
Schema
"schema": {
"type": "string",
"oneOf" : [
{
"minLength": 2
},
{
"maxLength": 4
}
]
}
API
var schema = json
.string()
.oneOf([json.minLength(2), json.maxLength(4)]);
Schema
"schema": {
"not": {"type": ["integer", "boolean"]}
}
API
var schema = json
.not(json.type(['integer', 'boolean']));
Schema
"schema": {"$ref": "http://json-schema.org/draft-04/schema#"}
API
var schema = json.$ref('http://json-schema.org/draft-04/schema#');