sfn-validator v0.1.6
SFN-Validator
Simple Friendly Node.js Validator.
Install
npm install sfn-validator --saveEnvironment
This package supports any version of Node.js that higher than 4.0.0.
Example
const Validator = require("sfn-validator");
var validator = new Validator({
name: {
type: "string",
required: true,
length: [3, 18]
},
email: {
type: "email",
required: true,
strict: true,
},
password: {
type: "string",
required: true,
length: [8, 18],
msg: {
required: "You must provide a 'password' in this form.",
equals: "The length of the 'password' must between 8 and 18.",
}
},
check_password: {
type: "string",
required: true,
equals: "password",
},
url: "url" // Short-hand, equal to {type: "url"}.
});
try{
validator.validate(data);
}catch(e){
console.log(e);
}API
new Validator(rules: any)Creates a validator with specified rules.validator.validate(data: any)Checks if the input data are all valid.- alias:
validator.check()
- alias:
validator.filter(data: any): anyFilters input data according to the rules.
Rules
Every field of rule can be defined with these properties:
typeThe data type to check, could be:stringnumberbooleanobjectCarries children fields.arrayemailurldateDate instance or a valid date string.timeUnix timestamp or time string.colorColor name, hex, RGB, RGBA color string.ipv4ipv6macMedia Access Control address (Machine address).uuidUniversal Unique Identifier.isbnInternational Standard Book Number.asciiOnly contains ASCII characters.base64Only contains base64 characters.jsondata-uri
requiredWhether the field is required (true) or not (false, default).equalsThe value of this field should be equal to the given field's.msgCustomize error messages, could be a string that sets all messages, or an object sets messages fortype,requiredandequals. If the field has alengthorrangeproperty, their error messages could be set as well.lengthOnly forstring,email,url,ascii,base64,json,data-uriandarray, could be a number sets an exact length, or an array sets the minimum and maximum length.rangeOnly fornumber, an array set the range (minimum and maximum) of the data value.strictStrict mode, only fornumber,boolean,email,url,ipv4,isbn, it'sfalseby default for most types exceptnumber, which istrueby default.childrenOnly forobject, to set children rules.
More Details About Strict Mode
numberWhen non-strict, treat numeric string as number.emailIf strict, the email address can only contain ASCII characters, and the hostname must not belocalhost.urlIf strict, the hostname can only contain ASCII characters, and the hostname must not belocalhost.ipv4If strict, reject private and reserved IP addresses.isbnIf strict, the book number must be prettified with hyphenates (-).
Pre-check of Rule Definition
When you call new Validator() and pass the rules, the program will perform
checking on the rules you provided, if any of them is invalid, an error will
be thrown and tells you where is incorrect in your rules, so that you can find
and fix it as soon as possible.
What Will Happen If Validating Failed?
If validating failed, validator.validate() will thrown an error message.
You can catch the error with a try...catch... block. This package will
modify the initial error stack and only shows you the useful parts, so it's
very friendly on user-experience.
About Children Rule
If you specify a field's type object, then this field can and must carry
children fields, like this example:
var rules = {
multi: {
type: "object",
children: {
name: "string",
email: "email"
}
}
};
// And input data should like this:
var data = {
multi: {
name: "John",
email: "john@example.com"
}
};Children rules can be nested and unlimited, BUT, if you use equals
property, be aware that it can only refers to the same depth of the rule.