js-data-schema
Define and validate rules, datatypes and schemata in Node and in the browser.
Install
Node
npm install --save js-data-schema
var Schemator = require('js-data-schema');
Browser
npm install --save js-data-schema
or bower install --save js-data-schema
Load js-data-schema/dist/js-data-schema.js
into your browser.
window.Schemator;
define(['js-data-schema'], function (Schemator) { ... })
var Schemator = require('js-data-schema');
Getting Started
var schemator = new Schemator();
schemator.defineSchema('Person', {
name: 'string'
});
var errors = schemator.validateSync('Person', { name: 'John' });
errors;
errors = schemator.validateSync('Person', { name: 50043 });
errors;
rule: 'type',
actual: 'number',
expected: 'string'
}
Status
Branch | Master |
---|
Bower | |
NPM | |
Build Status | |
Code Climate | |
Dependency Status | |
Coverage | |
API
Schemator
Schemator()
var schemator = new Schemator();
Schemator#availableDataTypes()
schemator.availableDataTypes();
Schemator#availableRules()
schemator.availableRules();
Schemator#availableSchemata()
schemator.defineSchema('PersonSchema', { ... });
schemator.availableSchemata();
Schemator#getDataType(name)
schemator.getDataType('myDataType');
Schemator#getRule(name)
schemator.getRule('myRule');
Schemator#getSchema(name)
schemator.getSchema('PersonSchema');
Schemator#removeDataType(name)
schemator.removeDataType('myDataType');
Schemator#removeRule(name)
schemator.removeRule('myRule');
Schemator#removeSchema(name)
schemator.removeSchema('PersonSchema');
Schemator#defineDataType(name, typeDefinition)
schemator.defineDataType('NaN', function (x) {
if (isNaN(x)) {
return null;
} else {
return {
rule: 'type',
actual: typeof x,
expected: 'NaN'
};
}
});
Schemator#defineRule(name, ruleFunc[, async])
schemator.defineRule('divisibleBy', function (x, divisor) {
if (typeof x === 'number' && typeof divisor === 'number' && x % divisor !== 0) {
return {
rule: 'divisibleBy',
actual: '' + x + ' % ' + divisor + ' === ' + (x % divisor),
expected: '' + x + ' % ' + divisor + ' === 0'
};
}
return null;
});
schemator.defineSchema('mySchema', {
seats: {
divisibleBy: 4
}
});
var errors = schemator.getSchema('mySchema').validateSync({
seats: 16
});
errors;
errors = schemator.getSchema('mySchema').validateSync({
seats: 17
});
errors;
Asynchronous rule:
schemator.defineRule('divisibleBy', function (x, divisor, cb) {
setTimeout(function () {
if (typeof x === 'number' && typeof divisor === 'number' && x % divisor !== 0) {
cb({
rule: 'divisibleBy',
actual: '' + x + ' % ' + divisor + ' === ' + (x % divisor),
expected: '' + x + ' % ' + divisor + ' === 0'
});
}
cb(null);
}, 1);
}, true);
schemator.defineSchema('mySchema', {
seats: {
divisibleBy: 4
}
});
var errors = schemator.getSchema('mySchema').validate({
seats: 16
}, function (err) {
errors;
errors = schemator.getSchema('mySchema').validate({
seats: 17
}, function (err) {
errors;
});
});
Schemator#defineSchema(name, definition)
schemator.defineSchema('PersonSchema', {
name: {
first: {
type: 'string',
maxLength: 255
},
last: {
type: 'string',
maxLength: 255
}
},
age: {
type: 'number',
max: 150,
min: 0
}
});
Schemator#validate(schemaName, attrs[, options], cb)
See Schema#validate(attrs[, options], cb)
Schemator#validateSync(schemaName, attrs[, options])
See Schema#validateSync(attrs[, options])
Schemator#setDefaults(schemaName, attrs)
See Schema#setDefaults(attrs)
Schemator#getDefaults()
See Schema#getDefaults()
Schemator#addDefaultsToTarget(schemaName, target[, overwrite])
See Schema#addDefaultsToTarget(target)
See Schema#stripNonSchemaAttrs(target)
Schema
Schema#validate(attrs[, options], cb)
PersonSchema.validate({
name: 'John Anderson'
}, function (err) {
err;
});
PersonSchema.validate({
name: 5
}, function (err) {
err;
});
Schema#validateSync(attrs[, options])
var errors = PersonSchema.validate({
name: 'John Anderson'
});
errors;
errors = mySchema.validate({
name: 5
});
errors;
Schema#setDefaults(attrs)
PersonSchema.setDefaults({
first: '',
last: '',
plan: 'free'
});
Schema#getDefaults()
PersonSchema.getDefaults();
first: '',
last: '',
age: 0
}
Schema#addDefaultsToTarget(target[, overwrite])
var person = {
first: 'John',
plan: 'premium'
};
PersonSchema.addDefaultsToTarget(person);
person;
first: 'John',
last: '',
plan: 'premium'
}
PersonSchema.addDefaultsToTarget(person, true);
person;
first: '',
last: '',
plan: 'free'
}
Schema#stripNonSchemaAttrs(target)
var person = {
first: 'John',
plan: 'premium',
nonSchema: 'value'
};
PersonSchema.stripNonSchemaAttrs(person);
person;
first: 'John',
plan: 'premium'
}
License
MIT License
Copyright © 2013-2014 Jason Dobry
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.