checky
v1.3.0
Published
Declarative JavaScript object validation.
Downloads
4
Maintainers
Readme
checky
Declarative JavaScript object validation.
Usage
var checky = require('checky');
var checkPerson = checky({
name: String,
age: {
type: Number,
min: 18,
max: 80
},
hobby: ['Running', 'Gaming', 'Hacking'],
other: {
type: String,
optional: true
}
});
var dennis = {
name: 'Dennis Ritchie',
age: 70,
hobby: 'Hacking'
};
checkPerson(dennis); // true
Recursive Schemas
var checkNested = checky({
name: String,
age: Number,
nested: {
type: Object,
fields: {
name: String,
age: Number,
nested: {
type: Object,
fields: {
name: String,
age: Number
}
}
}
}
});
var russianStallmans = {
name: 'Richard Stallman',
age: 61,
nested: {
name: 'Richard Stallman',
age: 61,
nested: {
name: 'Richard Stallman',
age: 61
}
}
};
checkNested(russianStallmans); // true
Options
The second argument to the schema checking function returned by checky is an options object. Options include:
- debug (true|false) - print error messages for invalid objects
- sparse (true|false) - all schema fields are made optional
- err (true|false) - instead of returning false, return an err object containing details of the schema violation