Define and validate rules, datatypes and schemata in Node and in the browser.
robocop.js is a library that allows you to define and validate rules, datatypes and schemata in Node and in the browser. Check it out:
robocop.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;
});
robocop.defineSchema('mySchema', {
seats: {
divisibleBy: 4
}
});
var errors = robocop.getSchema('mySchema').validateSync({
seats: 16
});
errors; // null
errors = robocop.getSchema('mySchema').validateSync({
seats: 17
});
errors; // {
// seats: {
// errors: [ {
// rule: 'divisibleBy',
// actual: '17 % 4 === 1',
// expected: '17 % 4 === 0'
// } ]
// }
// }
Another example:
robocop.defineSchema('person', {
name: {
given: {
type: 'string',
maxLength: 255
},
surname: {
type: 'string',
maxLength: 255
}
},
age: {
type: 'number',
max: 150,
min: 0
}
});
var errors = robocop.getSchema('person').validateSync({
name: {
given: 'John',
surname: 'Anderson'
},
age: 30
});
errors; // null
errors = robocop.getSchema('person').validateSync({
name: {
given: 'John',
surname: 'Anderson'
},
age: -4
});
errors; // {
// age: {
// errors: [{
// rule: 'min',
// actual: '150 < 0',
// expected: '150 >= 0',
// }]
// }
// }
Master | |
---|---|
Version | |
Source | master |
Build Status | |
Coverage | |
Code Climate | |
Dependency Status |
Installation - Read how to install robocop.js and get it running in your application.
Guide - Learn how to use robocop.js and read about common usage patterns.
API - Reference API documentation for robocop.js.
Mailing List - Ask questions and get help.
Issues - Found a bug? Submit an issue!
GitHub - View the source code for robocop.js.