kay
User-centered validation library.
Installation
npm instal kkay --save
Example
var kay = ;var schema = kay; var user = lastname: 'Do'; schema;// {// $invalid: true,// firstname: {// $invalid: true,// required: true,// minlength: true// },//// lastname: {// $invalid: true,// minlength: true// }// }
Motivation
I believe that the purpose of client-side validation should be to provide guidance to users but in no way to strictly validate data.
Having this in mind completely changed the way I conceived kay.
What is invalid from a code perspective is not necessarily wrong from the user side.
For example, with kay null
does not make a minlength
constraint to be invalid unless it is required.
Instead, null
, ''
and []
are considered to be "empty" values.
Documentation
kay's api is a just a serie of chainable functions.
kay.validate(value, [callback])
Run the value against the added constraint and return a map of errors or call callback with it.
Example
var name = kay;name;// {// $invalid: true,// minlength: true// } name;// true name;// {}
kay.required()
Add a constraint that checks if value is not empty (null
, undefined
, []
, ''
).
Example
var name = kay;name;// {// $invalid: true,// required: true// } name;// {}
kay.string()
Add a constraint that checks if value is a string. Note: empty values are valid "string".
Example
var name = kay;name;// {// $invalid: true,// string: true// } name;// {}
kay.number()
Add a constraint that checks if value is a number. Note: empty values are valid "number".
Example
var age = kay;age;// {// $invalid: true,// number: true// } age;// {}
kay.func()
Add a constraint that checks if value is a function. Note: empty values are valid "func".
Example
var callback = kay;callback;// {// $invalid: true,// func: true// } callback;// {}
kay.object()
Add a constraint that checks if value is a object. Note: empty values are valid "object".
Example
var user = kayobject;user;// {// $invalid: true,// object: true// } user;// {}
kay.array()
Add a constraint that checks if value is a array. Note: empty values are valid "array".
Example
var repos = kay;repos;// {// $invalid: true,// array: true// } repos;// {}
kay.bool()
Add a constraint that checks if value is a boolean. Note: empty values are valid "bool".
Example
var isOnline = kay;isOnline;// {// $invalid: true,// bool: true// } isOnline;// {}
kay.minlength(minimum)
Add a constraint that checks if value has a length equal or greater than minimum. Note: empty values are valid "minlength".
Example
var repos = kay;repos;// {// $invalid: true,// minlength: true// } repos;// {}
kay.maxlength(maximum)
Add a constraint that checks if value has a length equal or lower than maximum. Note: empty values are valid "maxlength".
Example
var repos = kay;repos;// {// $invalid: true,// maxlength: true// } repos;// {}
kay.min(minimum)
Add a constraint that checks if value is a number equal or greater than minimum. Note: empty values are valid "min".
Example
var age = kay;age;// {// $invalid: true,// min: true// } age;// {}
kay.max(maximum)
Add a constraint that checks if value is a number equal or lower than maximum. Note: empty values are valid "max".
Example
var age = kay;age;// {// $invalid: true,// max: true// } age;// {}
kay.pattern(regexp)
Add a constraint that checks if value matches regexp. Note: empty values are valid "regexp".
Example
var email = kay;email;// {// $invalid: true,// pattern: true// } email;// {}
kay.message(msg)
Define a message for the previously added constraint. This msg will be used instead of "true" in the error map. Note: there's no restriction to what msg can be, as long as it can be stored in an object.
Example
var name = kay ; kay// {// $invalid: true,// required: 'Please fill in your name'// } kay// {// $invalid: true,// minlength: 'Sorry but that name is too short.'// }
kay.defaultValue(value)
Define a default value that can be used in a schema.
Example
var user = kay; user;// {// name: 'John'// }
kay.schema(obj)
Create a schema that can be used to validate a structure.
Example
var user = kay;
schema.validate(value, [callback])
Equivalent of kay.validate but for schemas.
Example
var user = kay; user; // {// $invalid: true,// name: {// $invalid: true,// required: true// },// age: {// $invalid: true,// number: true// }// }
schema.values(value, [callback])
Return the valid and default values from the provided "value". If callback is provided, return the result of calling it with two arguments: an error map (from schema.validate) and the data.
Example
var user = kay; user;// {// lastname: 'Doe'// } user;// {// firstname: 'John',// lastname: 'Doe'// }