A brief data validation library.
// ES6
import validate from '@konglx/data-validate';
// ES5
const validate = require('@konglx/data-validate');
The type check is based on Object.prototype.toString.call(o).slice(8, -1).toLocaleLowerCase()
// Basic data types
validate('string', 'i am a string') // => true
validate('number', 1) // => true
validate('null', null) // => true
validate('undefined', undefined) // => true
// Object Array
validate('object', {}) // => true
validate('array', []) // => true
// instanceOf
function Dog() {}
const dog = new Dog();
validate(Dog, dog); // => true
// optional
validate('?:string', undefined) // => true
validate('?:string', 'ok') // => true
// object
validate({ a: 'string', b: 'number'}, {
a: 'i am a string',
b: 1,
}) // => true
// array
validate(['string'], ['a', 'b']) // => true
validate(['string'], ['a', 1]) // => false
// if you want two type in a Array
validate(['string|number'], ['a', 1])
// deep
validate([{
name: 'string',
girlFrends: [
{
name: 'string',
friends: [
{
name: 'string',
classmates: [
{
name: 'string',
hobbies: []
}
]
}
]
}
]
}], [{
name: 'xiaoming',
girlFrends: [{
name: 'zhang',
friends: [{
name: 'mao',
classmates: [
{
name: 'li',
hobbies: [],
}
]
}]
}],
}]) // => true
// If you want get the error path
validate({ ok: 'number' }, { ok: '9999' }, falseThrowError = true); // This will throw a Error with error path
- Empty Array
// can pass validate
validate(
{
friends: [{
name: 'string'
}],
},
{
names: [],
}
) // => true
// can't pass validate
validate(
{
friends: [{
name: 'string',
}],
},
{
names: [{
}],
}
) // => true
npm publish --access=public
Test Cover Istanbul
npm run cover