README
JSEN">
jsen (JSON Sentinel) validates your JSON objects using JSON-Schema.
Website: http://bugventure.github.io/jsen
Table of Contents
- Getting Started
- Performance & Benchmarks
- JSON Schema
- Format Validation
- External Schemas
- Errors
- Gathering Default Values
- In-Browser Usage
- Tests
- Contributing
- Issues
- Changelog
- License
Getting Started
Install through NPM in node.
$ npm install jsen --save
var jsen = require('jsen');
var validate = jsen({ type: 'string' });
var valid = validate('some value'); // true
Install through Bower in your HTML page.
$ bower install jsen
<script src="bower_components/jsen/dist/jsen.min.js"></script>
<script>
var validate = jsen({ type: 'string' }); // under window.jsen
var valid = validate('some value'); // true
</script>
Validation works by passing a JSON schema to build a validator function that can be used to validate a JSON object.
The validator builder function (jsen) throws an error if the first parameter is not a schema object:
try {
// cannot use this string as a schema
jsen('not a valid schema');
}
catch (e) {
console.log(e);
}
jsen will not throw an error if the provided schema is not compatible with the JSON-schema version 4 spec. In this case, as per the spec, validation will always succeed for every schema keyword that is incorrectly defined.
// this will not throw, but validation will be incorrect
var validate = jsen({ type: 'object', properties: ['string', 'number'] });
// validation erroneously passes, because keyword `properties` is ignored
var valid = validate({}); // true
If you need to validate your schema object, you can use a reference to the JSON meta schema. Internally, jsen will recognize and validate against the metaschema.
var validateSchema = jsen({"$ref": "http://json-schema.org/draft-04/schema#"});
var isSchemaValid = validateSchema({ type: 'object' }); // true
isSchemaValid = validateSchema({
type: 'object',
properties: ['string', 'number']
});
// false, because properties is not in correct format
Performance & Benchmarks
JSEN uses dynamic code generation to produce a validator function that the V8 engine can optimize for performance. Following is a set of benchmarks where JSEN is compared to other JSON Schema validators for node.
- json-schema-benchmark
- z-schema bencrhmark
- jsck benchmark
- themis benchmark
- cosmicrealms.com benchmark
More on V8 optimization: Performance Tips for JavaScript in V8
JSON Schema
To get started with JSON Schema, check out the JSEN schema guide.
For further reading, check out this excellent guide to JSON Schema by Michael Droettboom, et al.
JSEN fully implements draft 4 of the JSON Schema specification.
Format Validation
JSEN supports a few built-in formats, as defined by the JSON Schema spec:
date-timeuriemailipv4ipv6hostname
These formats are validated against string values only. As per the spec, format validation passes successfully for any non-string value.
var schema = { format: 'uri' },
validate = jsen(schema);
validate('invalid/uri'); // false - format validation kicks in for strings
validate({}); // true - does not kick in for non-strings
Custom Formats
JSEN additionally supports custom format validation. Custom formats are passed in options.formats as a second argument to the jsen validator builder function.
var schema = { format: 'uuid' },
uuidRegex = '^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[89abAB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}

