kay
User-centered validation library.
Installation
Example
var kay = require('kay');
var schema = kay.schema({
firstname: kay.required().minlength(3),
lastname: kay.minlength(3)
});
var user = {
lastname: 'Do'
};
schema.validate(user);
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.required().minlength(3);
name.validate('Jo');
name.validate('Jo', function isInvalid (errors) {
return errors.$invalid;
});
name.validate('John');
kay.required()
Add a constraint that checks if value is not empty (null
, undefined
, []
, ''
).
Example
var name = kay.required();
name.validate(null);
name.validate('John');
kay.string()
Add a constraint that checks if value is a string.
Note: empty values are valid "string".
Example
var name = kay.required();
name.validate(123);
name.validate('John');
kay.number()
Add a constraint that checks if value is a number.
Note: empty values are valid "number".
Example
var age = kay.required();
age.validate('John');
age.validate(123);
kay.func()
Add a constraint that checks if value is a function.
Note: empty values are valid "func".
Example
var callback = kay.func();
callback.validate('John');
callback.validate(function () {});
kay.object()
Add a constraint that checks if value is a object.
Note: empty values are valid "object".
Example
var user = kay.object();
user.validate('John');
user.validate({});
kay.array()
Add a constraint that checks if value is a array.
Note: empty values are valid "array".
Example
var repos = kay.array();
repos.validate('John');
repos.validate(['kay']);
kay.bool()
Add a constraint that checks if value is a boolean.
Note: empty values are valid "bool".
Example
var isOnline = kay.bool();
isOnline.validate('John');
isOnline.validate(true);
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.minlength(2);
repos.validate(['kay']);
repos.validate(['kay', 'react-kay']);
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.maxlength(1);
repos.validate(['kay', 'react-kay']);
repos.validate(['kay']);
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.min(24);
age.validate(12);
age.validate(32);
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.max(24);
age.validate(32);
age.validate(12);
kay.pattern(regexp)
Add a constraint that checks if value matches regexp.
Note: empty values are valid "regexp".
Example
var email = kay.pattern(/[^@]+@[^@]+/);
email.validate('johngmail.com');
email.validate('john@gmail.com');
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
.required().message('Please fill in your name')
.minlength(3).message('Sorry but that name is too short.');
kay.validate(null)
kay.validate('Jo')
kay.defaultValue(value)
Define a default value that can be used in a schema.
Example
var user = kay.schema({
name: kay.defaultValue('John')
});
user.values({});
kay.schema(obj)
Create a schema that can be used to validate a structure.
Example
var user = kay.schema({
name: kay.required().string(),
age: kay.number()
});
schema.validate(value, [callback])
Equivalent of kay.validate but for schemas.
Example
var user = kay.schema({
name: kay.required().string(),
age: kay.number()
});
user.validate({
age: 'John'
});
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.schema({
firstname: kay.required().string(),
lastname: kay.string().defaultValue('Doe')
});
user.values({
lastname: 123
});
user.values({
firstname: 'John'
});