A cross-browser / node.js validator with JSONSchema compatibility as the primary goal.
Example
The core of revalidator is simple and succinct: revalidator.validate(obj, schema):
var revalidator = require('revalidator'); console.dir(revalidator.validate(someObject, { properties: { url: { description: 'the url the object should be stored at', type: 'string', pattern: '^/[^#%&*{}\\:<>?\/+]+$', required: true }, challenge: { description: 'a means of protecting data (insufficient for production, used as example)', type: 'string', minLength: 5 }, body: { description: 'what to store at the url', type: 'any',
This will return with a value indicating if the obj conforms to the schema. If it does not, a descriptive object will be returned containing the errors encountered with validation.
{ valid: true // or false errors: [/* Array of errors if valid is false */] }
In the browser, the validation function is exposed on window.validate by simply including revalidator.js.
Installation
Installing npm (node package manager)
$ curl http://npmjs.org/install.sh | sh
Installing revalidator
$ [sudo] npm install revalidator
Usage
revalidator takes json-schema as input to validate objects.
revalidator.validate (obj, schema, options)
This will return with a value indicating if the obj conforms to the schema. If it does not, a descriptive object will be returned containing the errors encountered with validation.
{ valid: true // or false errors: [/* Array of errors if valid is false */]}
Available Options
validateFormats: Enforce format constraints (default true)
validateFormatsStrict: When validateFormats is true treat unrecognized formats as validation errors (default false)
validateFormatExtensions: When validateFormats is true also validate formats defined in validate.formatExtensions (default true)
additionalProperties: When additionalProperties is true allow additional unvisited properties on the object. (default true)
cast: Enforce casting of some types (for integers/numbers are only supported) when it's possible, e.g. "42" => 42, but "forty2" => "forty2" for the integer type.
Schema
For a property an value is that which is given as input for validation where as an expected value is the value of the below fields
required
If true, the value should not be undefined
{ required: true }
allowEmpty
If false, the value must not be an empty string
{ allowEmpty: false }
type
The type of value should be equal to the expected value