A validator for HTTP request parameters.
npm i @killara/validation -S
- It supports three kinds of rule declarations. String inline rules, object rules and array rules.
- It supports async validation rule. For example, unique validation for only specific record in database table.
- It supports custom rules
- It supports custom error messages, or it uses default messages.
const Validation = require('@killara/validation');
const validation = new Validation();
const values = {
username: 'admins',
password: 'abcdef',
sex: 'male',
};
const rules = {
username: 'required|alpha:6',
password: {
required: true,
regexp: /^[a-z]{6,18}$/,
},
sex: [ 'male', 'female' ],
};
const messages = {
'username.alpha': 'The field must be entirely alphabetic characters',
'sex.in': 'The field should be included in the list of ${ _items_.join(", ") }'
};
const errors = await validation.validate(values, rules, messages);
if (!errors) {
// all validations passed
} else {
// we got an array of errors
}
-
accepted
- string style:
field: 'accepted'
- object style:
field: { accepted: true }
- string style:
-
alpha
- string style:
field: 'alpha:6'
orfield: 'alpha:len=6'
- object style:
field: { alpha: { len: 6 } }
- string style:
-
alphanum
- string style:
field: 'alphanum:6'
orfield: 'alphanum:len=6'
- object style:
field: { alphanum: { len: 6 } }
- string style:
-
confirmed
Rule
confirmed
: thefield
need to have the same value as the value that be filled byfield_confirmed
. We can changefield_confirmed
to any names withconfirmed:"custom"
- string style:
field: 'confirmed'
orfield: 'confirmed"custom_field_name"'
- object style:
field: { confirmed: { len: 6 } }
- string style:
-
date
- string style:
field: 'date'
- object style:
field: { date: true }
- string style:
-
datetime
- string style:
field: 'datetime'
- object style:
field: { datetime: true }
- string style:
-
time
- string style:
field: 'time'
- object style:
field: { time: true }
- string style:
-
email
- string style:
field: 'email:true'
- object style:
field: { email: true }
- string style:
-
in
array
style:field: [ 'basketball', 'football' ]
- object style:
field: { in: [ 'basketball', 'football' ] }
-
money
- string style:
field: 'money'
orfield: 'money:0'
field: 'money:2'
(default) - object style:
field: { money: { decimal: true } }
orfield: { money: { decimal: 0 } }
orfield: { money: { decimal: 2 } }
- string style:
-
numeric
- string style:
field: 'numeric:6'
orfield: 'numeric:len=6'
- object style:
field: { numeric: { len: 6 } }
- string style:
-
regexp
- string style:
field: 'regexp:"^123456$"'
- object style:
field: { regexp: new RegExp(/abc/, 'i') }
orfield: { regexp: /^[0-9a-zA-z]{8,16}$/ }
- string style:
-
required
- string style:
field: 'required'
orfield: 'required:true'
- object style:
field: { required: true }
- string style:
- #constructor(options?: object)
- Initialize with options (cant include
options
properties)
- Initialize with options (cant include
- #async validate(params: object, rules?: object, messages?: object)
- Validate params
- #addRule(name: string, ruleFunc: ruleFunc: (field: string) => (context: object) => (params: object) => bool)
- Add custom rule
- #addMessage(name: string, message: string)
- Add custom message