@pinkyo/validatorjs
v0.0.9
Published
central validator to provide an alternative solution.
Downloads
5
Readme
validatorjs
validatorjs gives you nearly total control when you validate form values, compare to the existing validator tools. You can use it as a part to wrap the <input>, <textarea> and so on, with an error message display. although I try to emit convention usage, I can't get away from all and there are stil some convention usages.
Usage
$npm i @pinkyo/validatorjs -S
Document
this module includs several simple concepts.
field
: field to validate, including id, name, groups, getter.id
: field id.required
.name
: field name for error message display.optional
. if not specified, id will be used.groups
: groups that field is in, it is used when check more than one field.optional
. we can validate a group of fields a time, and all fields will be added to default group when registered.getter
: function to get field value, without no parameter.required
.
validationChain
: an array of functions to validate the field value. The following is a validation example:({name, value}) => { if (value < 10) return; return `${name} must less than 10`; }
listener
: function that will be invoked when validation is triggered.resultCache
: after validation, result will be saved to a cache for future's operataion, like access.
API
create a validator
A validator is the beginning of validatorjs usage, we operate on this object for the function we required.
function createValidator();
register a field to validator
function register(field, validationChain, callback);
validate groups, if not specified, default group. defalt group contains all fields.
function validate(groups, callback);
validate one field
function validateOne(id, callback) {
subscribe a listener
function subscribe(id, listener, callback);
clear all listen to a field
function clearListeners(id, callback);
update groups of a field
function updateGroups(id, groups, callback);
add a group to a field
function addGroup(id, group, callback);
remove a group to a field
function removeGroup(id, group, callback);
get one latest valiation result from result cache by id
function getOneResult(id);
get latest valiation results from result cache by groups
function getResults(groups);
clear result from result cache
function clearOneResult(id);
clear results from result cache by groups
function clearResults(groups);
deregister a field
function deregister(id, callback);
print validation info
function printValidationInfo();
print group info
function printGroupInfo();
print all info
function printAllInfo();
Example
The following is an example:
const id = 'test.id';
const group = 'test.group';
const invalidGroup = "test.group.invalid";
const groups = [group];
const name = 'test.name';
const getter =() => 5;
const tip = 'value must less than 5.';
const validationChain = [({name, value}) => value < 5? '': tip];
const field = {id, groups, getter, name};
const validator = createValidator();
validator.register(field, validationChain);
validator.addGroup(id, invalidGroup);
const result = validator.validate([invalidGroup]);
Tools
we provide a tools diretory that contains some commomly used function to reduce workload. and it's alternative. Tools
Contribution
report issue and pull request are welcomed.
LICENSE
MIT