Dead simple sync/async object validation library with focus on custom matchers.
Install
npm i entity-validator
How to use?
validate(params)
params.entity: Entity params.matchers: Array, params.attributes: Array,
You have entity for validate
const entity = {};
and attributes rules
const attributes = key: 'name' rules: 'presence' // or [ { key: 'presence', params: {} } ] ;
and matchers which used in rules
const presenceMatcher = key: 'presence' { if !attribute || !entity || entityattribute return null; return messageKey: 'presenceText' ; }; const matchers = presenceMatcher ;
now you can get validation matchers errors!
const validate = ;const matchersErrors = await ; // > matchersErrors// [// {// key: 'presence',// attribute: 'name',// checkResult: { messageKey: 'presenceText' },// },// ]
Helpers
createAttributesByValidator(validator, params)
Tranform validator object (groupped attributes) to attributes.
const createAttributesByValidator = ; const validator = key: 'user' key: 'email' rules: 'presence' 'length' min: passwordMinLength max: passwordMaxLength ; const params = passwordMinLength: 6 passwordMaxLength: 12 ; const attributes = ; // attributes => [ // { // key: 'email', // rules: [ // 'presence', // ['length', { min: 6, max: 12 }], // ] // }, // ]);
Flow types
;