1.0.6 • Published 9 years ago

pierro-validate v1.0.6

Weekly downloads
8
License
MIT
Repository
github
Last release
9 years ago

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: "user@domain.com",
	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("user@domain.org"))
	console.log("Valid email address");
else 
	console.log("This is not a valid email address");