README
skeemas
Comprehensive JSON Schema (drafts 3 and 4) validation.
Installation
npm install skeemas --save
Basic Validation
skeemas.validate(subject, schema[, options])
var skeemas = require('skeemas');
skeemas.validate('foo', { type:'string' }).valid; // true
skeemas.validate(10000, { type:'string' }).valid; // false
skeemas.validate(10000, { type:'number' }).valid; // true
// Result contains an array of errors
var result = skeemas.validate('test', { enum:['foobar'], minLength:5 });
result.valid; // false
result.errors; // array with 2 error objects
// Pass the "breakOnError" option to stop processing on the first error
var result = skeemas.validate('test', { enum:['foobar'], minLength:5 }, { breakOnError:true });
result.valid; // false
result.errors; // array with 1 error object
var result = skeemas.validate({
foo: 'bar',
nested: {
stuff: [1,2,3],
ignoreMe: 'undeclared property'
}
}, {
properties: {
foo: { type:'string' },
nested: {
properties: {
stuff: {
type: 'array',
items: { type:'integer' }
}
// We aren't going to declare `ignoreMe`. To disallow extra
// props we could set `additionalProperties:false`.
}
}
}
});
result.valid; // true
assert.deepEqual(result.cleanInstance, {
foo: 'bar',
nested: {
stuff: [1,2,3]
// notice the `ignoreMe` property is removed from `cleanInstance`
}
});
For more information about constructing schemas see http://json-schema.org/ or the wonderful guide at http://spacetelescope.github.io/understanding-json-schema/index.html
Adding Schemas
Skeemas supports validation by schema id and refrences between schemas via the $ref
property:
// Create an instance of a validator
var validator = require('skeemas')();
// Add schemas to the validator
validator.addRef({ type:'string', pattern:'^[a-z0-9]+