@5alid/schema
v0.1.5
Published
model validation utility, similar to mongoose `schema` but for general usage, mainly to validate objects for noSQL databases
Downloads
15
Readme
Installation
npm npm i @5alid/schema
yarn yarn add @5alid/schema
Usage
import createSchema from '@5alidz/schema';
const Person = createSchema({
name: {
type: 'string',
minLength: 5,
maxLength: 20,
},
age: {
type: 'number',
min: 13,
max: 120,
},
pets: {
type: 'array',
optional: true,
of: {
type: 'object',
shape: {
name: {
type: 'string',
},
animal: {
type: 'string',
},
},
},
},
});
const errors = Person.validate({
name: 'John Doe',
age: 42,
pets: [
{ name: 'fluffy', animal: 'dog' },
{ name: 'pirate', animal: 'parrot' },
],
});
console.log(errors); // -> null
const errors2 = Person.validate({
name: true,
age: '42',
});
console.log(errors2); // -> [ { key: 'name', message: 'expected string but received "boolean"' }, { key: 'age', message: 'expected number but received "string"' }]