To install the stable version:
npm install --save uni-validator
import Validator from 'uni-validator';
const validator = new Validator(); // instance with default rules
const validator = new Validator(myRules); // or instance with custom rules
// gonna override default ones with same names (see link below)
.validate(stringToValidate, arrayOfRules)
validator.validate('someValue', [{
name: 'minLength',
len: 10
},{
name: 'equals',
to: 'someValueee'
}])
for each rules which didnt passed validation equal pair of key-value will be created in results object
results: {
minLength: 'minimum of 10 characters',
equals: 'should be equal to someValueee'
},
valid: false
results: {},
valid: true
.validateGroup(groups)
validator.validateGroup([
{
data: 'someValue1', // value to validate
rules: [
{
name: 'minLength',
len: 8
}
],
field: 'value1' // optional (will fallback to value of data key (currently 'someValue1')), provide it if you want other key-name of current value in returned validation results
}, //second value
{
data: 'someValue2',
rules: [
{
name: 'minLength',
len: 12
},{
name: 'equals',
to: 'someValueee',
toAlias: 'password'
}
]
}])
{
errorObjects: {
value1: {
results: {},
valid: true
},
someValue2: {
results: {
equals: 'should be equal to password',
minLength: 'minimum of 12 characters'
},
valid: false
}
},
valid: false
}
Both examples above will run paralell validation. If you like to validate with some rule only if previous validation ended with success (for example you want to check if your value equals to another one only in case if this value exist) you should add next reference to rule:
// will run 'equals' check only if value 'someValue' isnt empty
// same for '.validateGroup' method
// you can chain 'next:' as many times as you want
validator.validate('someValue',
[{
name: 'empty',
next: {
name: 'equals',
to: 'someValueee'
}
}, {
name: 'minLength',
len: 8
}])
.addRule(name, rule, errorMsg)
validator.addRule(
'myNewRule', // value to validate
(val, options) => val !== options.notEqual, // function(val, options) - if returns true after validation = rule marked as valid
(val, options) => `${ val } shouldn't be equal to ${ options.notEqual }` //can be function(val, options) or string - text to show if validation fails
);
//or
validator.addRule(
'myNewRule2',
val => val !== '2',
"shouldn't be equal to 2"
);
In order to use your new rules or old ones you should pass them in array as second argument of .validate or .validateGroup (for each instance to validate) method. Invocation of method will go though all rules and if a single rule will fail validation it will return process mareked as valid: false with messages from all rules failed during validation. You can pass em as objects with structure
[
{
name: 'ruleName', // name of rule (first argument of .addRule method)
...customValues // set of key:values which used to determine if validation process went success. Will be inside options object.
},
...otherObjectRules
]
or you can pass them as simple strings if rule doesnt require additional params to compare
['empty', 'isNumber', ...otherStrings]
https://github.com/N1gma/uni-validator/blob/master/src/index.js
https://github.com/N1gma/uni-validator/blob/master/src/validatorRules/commonRules.js