checker
Checker is the collection of common abstract node.js methods for validatiors and setters.
Usage
npm install checker --save
var checker = ;
Synopsis
;
mixed
err Object
results The parsed object.
Object
details {
<name>: <detail>
}
detail.value
mixed
the parsed valuedetail.is_default
Boolean
if the current property is defined inschema
, but the input data doesn't have it, then the value will betrue
detail.is_cooked
Boolean
if there're any setters, it will betrue
detail.origin
the origin value of the propertydetail.error
the error belongs to the current property. If not exists, it will benull
Validation, Error Messages
Simple synchronous validators
var schema = username: { return /^[a-zA-Z0-9]{6,}$/; } message: 'Username must only contain letters, numbers; ' + 'Username must contain at least 6 charactors' ; var c = ; c;
Regular expressions as validators
The error hint of the example above is bad, because we want to know the very certain reason why we are wrong.
The schema
below is equivalent to the one of the previous section:
validator: { return value && valuelength > 5; } /^[a-zA-Z0-9]+$/ message: 'Username must contain at least 6 charactors' 'Username must only contain letters and numbers' ;
Asynchronous validators
{ var done = this; // this is an async method, and takes sooooo long... ; }
Programmatical Details
Options
String
options.default_message Default error message
Boolean=false
options.parallel By default, checker
will check each properties in series,
Boolean=false
options.limit If options.limit
is true
and a certain property of the input data is not defined in the schema
, the property will be removed.
Default to false
.
Boolean=false
options.check_all By default, checker
will exit immediately at the first error. But if options.check_all
is true
, it will parse all the properties, and collect every possible error.
Object
options.context See sections below.
Schema Structures
<name>: <rule>
Where rule
might contains (all properties are optional):
validator
RegExp
The regular exp that input must matches againstFunction
Validation function. Ifarguments.length === 3
, it will be considered as an async methodsArray.<RegExp|Function>
Group of validations. Asks will check each validator one by one. If validation fails, the rest validators will be skipped.- See sections above for details
Function|Array.<Function>
setter See sections above for details.
String
message Default error message
String
default: this
object inside validators and setters
Inside validators(rule.validator
) and setters(rule.setter
), there're several opaque methods
this.async()
Generate the done
function to make the validator or setter become an async method.
var done = this.async();
For details, see the demos above.
this.get(name)
The value of the input object by name
this.set(name, value)
Change the value of the specified property of the input object.
{
username: {
},
password: {
validator: function(value){
var username = this.get('username');
// Guests are welcome even without passwords
return value || username === 'guest';
}
}
}
Notice that you'd better use this.get
and this.set
with the options.parallel
setting as false
(the default value). Otherwise, it might encounter unexpected situations, because the value of the object is ever changing due to the setter.
So, use them wisely.
Object
this.context The options.context
itself.