constructor([options]) - new Class Parameter instance
options.translate - translate function
options.validateRoot - config whether to validate the passed in value must be a object, default to false.
options.convert - convert primitive params to specific type, default to false.
options.widelyUndefined - convert empty string(''), NaN, Null to undefined, this option can make rule.required more powerful, default to false.This may change the original input params.
validate(rule, value) - validate the value conforms to rule. return an array of errors if break rule.
addRule(type, check) - add custom rules.
type - rule type, required and must be string type.
check - check handler. can be a function or a RegExp.
Note: when options.convert enabled, all built-in rules check for primitive input param and convert it to rule's default convertType(which defined below), you can also enable this feature for specific rule by convertType options in each rule definition.
Example
var Parameter = require('parameter');var parameter = new Parameter({ translate: function() { var args = Array.prototype.slice.call(arguments); // Assume there have I18n.t method for convert language. return I18n.t.apply(I18n, args); }, validateRoot: true, // restrict the being validate value must be a object});var data = { name: 'foo', age: 24, gender: 'male'};var rule
required - if required is set to false, this property can be null or undefined. default to true.
type - The type of property, every type has it's own rule for the validate.
convertType - Make parameter convert the input param to the specific type, support int, number, string and boolean, also support a function to customize your own convert method.
default - The default value of property, once the property is allowed non-required and missed, parameter will use this as the default value. This may change the original input params.