pierro-validate
v1.0.6
Published
Node / Javascript Validator
Downloads
2
Readme
pierro-validate
Validation library for Node / browser
First install package
npm install --save pierro-validate
Common use
var Validator = require("pierro-validate");
var TestData = {
userName: "yourusername",
userPass: "youruserpass",
userEmail: "[email protected]",
userStatus: "enabled",
userCreatedon: new Date().getTime()
};
Validate = new Validator(TestData);
Validate.name("userName", "Username");
Validate.rule("userName", "required");
Validate.rule("userName", "alphanum");
Validate.rule("userName", "minLength", "6");
Validate.rule("userName", "maxLength", "100");
Validate.name("userPass", "Password");
Validate.rule("userPass", "required");
Validate.rule("userPass", "minLength", "6");
Validate.rule("userPass", "maxLength", "100");
Validate.name("userEmail", "Email");
Validate.rule("userEmail", "email");
Validate.name("userStatus", "Status");
Validate.rule("userStatus", "isEither", ["enabled", "disabled", "blocked", "removed"]);
Validate.name("userCreatedon", "Created on");
Validate.rule("userCreatedon", "timestamp");
Validate.run().then(function(){
console.log("Validation succes!");
}).catch(function(errors){
console.log("Validation failed:");
console.log(errors);
});
Methods
initialize class
Validate = new Validator();
isString
if (Validate.isString("test"))
console.log("Valid string");
else
console.log("This is not a valid string");
isArray
if (Validate.isArray([1, 2, 3]))
console.log("Valid array");
else
console.log("This is not a valid array");
isObject
if (Validate.isObject({}))
console.log("Valid object");
else
console.log("This is not a valid object");
isDate
if (Validate.isDate(new Date()))
console.log("Valid date object");
else
console.log("This is not a valid date object");
isTimestamp
if (Validate.isTimestamp(new Date().getTime()))
console.log("Valid timestamp");
else
console.log("This is not a valid timestamp");
isIp
if (Validate.isIp("192.168.0.1"))
console.log("Valid IP address");
else
console.log("This is not a valid IP address");
isEmail
if (Validate.isEmail("[email protected]"))
console.log("Valid email address");
else
console.log("This is not a valid email address");