validatorjs

Travis Coveralls npm (scoped) npm npm

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.

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