klay
Isomorphic and extensible validation library for JavaScript.
API Documentation
Usage
Install
npm install --save klay-core
Validate
const klay = require('klay-core').defaultModelContext
const myModel = klay
.object()
.children({
firstName: klay.string().required(),
lastName: klay.string().required(),
email: klay.email().required(),
age: klay.integer(),
})
.strict()
const results = myModel.validate({
firstName: 'John',
lastName: 42,
email: 'invalid.com',
age: 'eleven',
})
console.log(results.toJSON())
{ value:
{ firstName: 'John',
lastName: '42',
email: 'invalid.com',
age: 'eleven' },
conforms: false,
errors:
[ { message: 'expected value (invalid.com) to be email',
path: ['email'] },
{ message: 'expected value (eleven) to have typeof number',
path: ['age'] } ] }
Extend
const klay = require('klay-core').defaultModelContext
klay.use({
types: ['custom-type'],
defaults: {required: true, strict: true}
})
const myModel = klay
.object()
.children({
firstName: klay.string(), // required by default
lastName: klay.string(), // required by default
email: klay.email(), // required by default
age: klay.integer().optional(),
custom: klay.customType(),
})