- think-validator
- Useage
- Add Custom Valid Method
- Parse Rule's Arguments
- Param Validation Config
- Supported Data Type
- Nested Validation Type
- Custom Error Message
- Supported Validation Type
- requiredIf: [Array]
- requiredNotIf: [Array]
- requiredWith: [Array]
- requiredWithAll: [Array]
- requiredWithOut: [Array]
- requiredWithOutAll: [Array]
- contains: [String]
- equals: [String]
- different: [String]
- before: [true|date format string]
- after: [true|date format string]
- alpha: [true]
- alphaDash: [true]
- alphaNumeric: [true]
- alphaNumericDash: [true]
- ascii: [true]
- base64: [true]
- byteLength: [{min: 0, max: 10}]
- creditCard: [true]
- currency: [true|options]
- date: [true]
- decimal: [true]
- divisibleBy: [number]
- email: [true|options]
- fqdn: [true|options]
- float: [true|{min: 0, max: 10}]
- fullWidth: [true]
- halfWidth: [true]
- hexColor: [true]
- hex: [true]
- ip: [true]
- ip4: [true]
- ip6: [true]
- isbn: [true]
- isin: [true]
- iso8601: [true]
- in: [Array]
- notIn: [Array]
- int: [true|{min: 0, max: 10}]
- length: [{min: 0, max: 10}]
- lowercase: [true]
- uppercase: [true]
- mobile: [true|locale]
- mongoId: [true]
- multibyte: [true]
- url: [true|options]
- field: [true]
- field: [true]
- image: [true]
- startWith: [String]
- endWith: [String]
- string: [true]
- array: [true]
- boolean: [true]
- object: [true]
- regexp: [Regexp]
- issn: [true]
- uuid: [true]
- md5: [true]
- macAddress: [true]
- numeric: [true]
- dataURI: [true]
- variableWidth: [true]
import Validator from 'think-validator';
let instance = new Validator(ctx);
let ret = instance.validate(rules, msgs);
ctx
: the request data.rules
: the validation rules.msgs
: the custom error messages.
If valid ok, the ret
is {}, else ret
is output like {param1: 'error message', ...}.
let instance = new Validator(ctx);
instance.add('custom-valid', function(value, options) {
return value === 'thinkjs';
}, 'this id default error message');
let ret = instance.validate(rules);
You can parse the rule's arguments with ctx before validation. Just like this to add parse function for custom-valid:
let instance = new Validator(ctx);
instance.add('_custom-valid', function(arg, ctx) {
return newarg
};
Validation rules is written in json like this:
let rules = {
id: {
int: true,
required: true,
trim: true,
default: 12,
method: 'get'
}
}
Param is not required
by default, so if you need param not empty, you should assign required
with true
.
If you want trim
the space for the param you should assign trim
with true
,for example, if the id's value is '12 ' and trim: true
then id
is an integer, but it won't with trim: false
.
With default
you can give the param default value, if param's value is true empty, it will be the default value.
With method
you can assign in which type we get the param, get
,post
,file
is supported.
The supported data types include boolean,string,int,float,array,object. And the default type is string.
When valid type int
is true, if pass the validation the param's value will auto convert into integer.
When valid type float
is true, if pass the validation the param's value will auto convert into float.
When valid type boolean
is true, ['yes', 'on', '1', 'true', true]
will auto convert into true
, and others to false
.
When valid type array
is true and param's value is not array, it will convert param's value to [param's value]
.
Nested validation will valid the item in array or in object. But it only support the first layer child validation.
let rules = {
array: true,
children: {
int: true,
trim: true,
required: true
}
}
let rules = {
object: true,
children: {
int: true,
trim: true,
required: true
}
}
let rules = {
id: {
int: true
}
}
It will find the matched error message with valid failed by order:
-
- 'int': 'error message'
-
- 'id': 'error message'
-
- 'id': { int: 'error message' }
when the rule is object nested,
-
- 'param': { 'param1[,param2]': 'error message' }
-
- 'param': { 'param1[,param2]': { int: 'error message' } }
And the priority is 5 > 4 > 3 > 2 > 1.
If the requiredIf
's argument's first item has value in request data, let the first item is the value(in request data).
If the requiredIf
's argument's first item does not have value in request data, let the first item keep intact.
If the first item is in the last items, the param's value is required.
If the requiredNotIf
's argument's first item has value in request data, let the first item is the value(in request data).
If the requiredNotIf
's argument's first item does not have value in request data, let the first item keep intact.
If the first item is not in the last items, the param's value is required.
When some items of requiredWith
's arugument is not true empty in request data, the param's value is required.
When all items of requiredWithAll
's arugument is not true empty in request data, the param's value is required.
When some items of requiredWithOut
's arugument is true empty in request data, the param's value is required.
When all items of requiredWithOutAll
's arugument is true empty in request data, the param's value is required.
If the contains
's argument has value in request data, the rule will check if aram's value contains the value(in request data).
If the contains
's argument does not have value in request data, the rule will check if param's value contains equals
's argument.
If the equals
's argument has value in request data, the rule will check if the value(in request data) equal param's value.
If the equals
's argument does not have value in request data, the rule will check if equals
's argument equal param's value.
If the equals
's argument has value in request data, the rule will check if the value(in request data) not equal param's value.
If the equals
's argument does not have value in request data, the rule will check if equals
's argument not equal param's value.
Check if param's value before the giving date.
If before
= true, the giving date is now
.
Check if param's value after the giving date.
If after
= true, the giving date is now
.
Check if param's value contains only letters (a-zA-Z).
Check if param's value contains only letters (a-zA-Z_).
Check if param's value contains only letters, numbers.
Check if param's value contains only letters, numbers and _.
Check if param's value is ascii.
Check if param's value is base64.
Check if param's value length(in bytes) falls in a range.
Check if param's value is creditCard.
Check if param's value is currency format.
options
please see validator.js.
Check if param's value is date format.
Check if param's value represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.
Check if param's value is a number that's divisible by the giving one.
Check if param's value is an email.
options
please see validator.js.
Check if param's value is fqdn.
options
please see validator.js.
If float
= true, check if param's value is a float.
If float
= {min: 0, max: 10}, check if param's value is a float between min
and 'max'.
Check if param's value contains any full-width chars.
Check if param's value contains any half-width chars.
Check if param's value is a hexadecimal color.
Check if param's value is a hexadecimal number.
Check if param's value is an ip4 or ip6.
Check if param's value is an ip4.
Check if param's value is an ip6.
Check if param's value is an isbn.
Check if param's value is an ISIN (stock/security identifier).
Check if param's value is a valid ISO 8601 date.
Check if param's value is in a array of allowed values.
Check if param's value is not in a array of allowed values.
If int
= true, check if param's value is an integer.
If int
= {min: 0, max: 10}, check if param's value is an integer between min
and 'max'.
Check if param's value length falls in a range.
Check if param's value is lowercase.
Check if param's value is uppercase.
Check if param's value is a mobile phone number.
locale
please see validator.js.
Check if param's value is a valid hex-encoded representation of a MongoDB ObjectId.
Check if param's value contains one or more multibyte chars.
Check if param's value is an URL.
options
please see validator.js.
Check if param's value is a sql order string.
Check if param's value is a sql field string.
Check if param's value is an image file.
Check if param's value start with the giving string.
Check if param's value end with the giving string.
Check if param's value is string.
Check if param's value is array.
If param's value is not array, it will convert to [param's value]
.
Check if param's value is boolean.
If param's value is one of ['yes', 'on', '1', 'true', true], it will convert to true
, and others will convert to false
.
Check if param's value is object.
Check if param's value match the regexp.
Check if param's value is an ISSN.
Check if param's value is a UUID (version 3, 4 or 5).
Check if param's value is md5.
Check if param's value is macaddress.
Check if param's value contains only numbers.
Check if param's value is a data uri format.
Check if param's value contains a mixture of full and half-width chars.