A cross-browser / node.js validator used by resourceful and flatiron.
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',
default: null
}
}
}));
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
.
$ curl http://npmjs.org/install.sh | sh
$ [sudo] npm install revalidator
revalidator
takes json-schema as input to validate objects.
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 */]
}
- validateFormats: Enforce format constraints ( default:
true
) - validateFormatsStrict: When
validateFormats
istrue
treat unrecognized formats as validation errors ( defaultfalse
) - validateFormatExtensions: When
validateFormats
istrue
also validate formats defined invalidate.formatExtensions
. This option is used for lx-valid format extensions and additional custom formats. Those are stored in here. ( 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. ( default:undefined
) - deleteUnknownProperties: Deletes all properties from object which are not declared in the schema. ( default:
false
) - convert: Converts a property by the format defined in the schema. Modifies the original object. ( default:
undefined
) - trim: Trims all properties of type
string
. Modifies the original object. ( default:false
) - strictRequired: Sets validity of empty
string
tofalse
. ( default:false
)
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
If true, the value should not be empty
{ required: true }
The type of value
should be equal to the expected value
{ type: 'string' }
{ type: 'number' }
{ type: 'integer' }
{ type: 'array' }
{ type: 'boolean' }
{ type: 'object' }
{ type: 'null' }
{ type: 'any' }
{ type: ['boolean', 'string'] }
The expected value regex needs to be satisfied by the value
{ pattern: /^[a-z]+$/ }
The length of value must be greater than or equal to expected value
{ maxLength: 8 }
The length of value must be lesser than or equal to expected value
{ minLength: 8 }
Value must be greater than or equal to the expected value
{ minimum: 10 }
Value must be lesser than or equal to the expected value
{ maximum: 10 }
Value must be greater than expected value
{ exclusiveMinimum: 9 }
Value must be lesser than expected value
{ exclusiveMaximum: 11 }
Value must be divisible by expected value
{ divisibleBy: 5 }
{ divisibleBy: 0.5 }
Value must contain more then expected value number of items
{ minItems: 2 }
Value must contains less then expected value number of items
{ maxItems: 5 }
Value must hold a unique set of values
{ uniqueItems: true }
Value must be present in the array of expected value
{ enum: ['month', 'year'] }
Value must be a valid format
{ format: 'url' }
{ format: 'email' }
{ format: 'ip-address' }
{ format: 'ipv6' }
{ format: 'date-time' }
{ format: 'date' }
{ format: 'time' }
{ format: 'color' }
{ format: 'host-name' }
{ format: 'utc-millisec' }
{ format: 'regex' }
{ format: ['ip-address', 'ipv6'] }
Value must conform to constraint denoted by expected value
{ conform: function (v) {
if (v%3==1) return true;
return false;
}
}
{
name: {
type: 'string'
},
verifiedName: {
type: 'string',
conform: function (actual, original) {
if (actual === original.name) {
return true;
}
return false;
}
}
}
Value is valid only if the dependent value is valid
{
town: { required: true, dependencies: 'country' },
country: { maxLength: 3, required: true }
}
We also allow nested schema
{
properties: {
title: {
type: 'string',
maxLength: 140,
required: true
},
author: {
type: 'object',
required: true,
properties: {
name: {
type: 'string',
required: true
},
email: {
type: 'string',
format: 'email'
}
}
}
}
}
We also allow custom message for different constraints
{
type: 'string',
format: 'url'
messages: {
type: 'Not a string type',
format: 'Expected format is a url'
}
{
conform: function () { ... },
message: 'This can be used as a global message'
}
Converts a property by the format defined in the schema and returns the converted object in the result.
var data = {
birthdate: '1979-03-01T15:55:00.000Z'
};
var schema = {
properties: {
birthdate: {
type: 'string',
format: 'date-time'
}
}
};
var convertFn = function(format, value) {
if (format === 'date-time') {
return new Date(value);
}
return value;
};
var result = validate(data, schema, {convert: convertFn});
// birthdate was converted
typeof result.convertedObject === 'object';
typeof result.convertedObject.birthdate === 'object';
All tests are written with vows and should be run with npm:
$ npm test