1.5.4 • Published 6 years ago

full-validator v1.5.4

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

#The full Validator

##Easy Validator with style query

Easy to use utility to create a system of expressive validations by sentences. This library is inspired by the classic library of field validations, uses a sentence formed by one or several pipes, with very simple and expressive syntax, it is easy, to use, and very useful in the validation of http requests or fields of forms in the browser side. One of the advantages of full-validator is that it uses the node validator module that works in the client as in the server, and the documentation of this help even to understand the possibilities of full-validator.

#Examples:

##basic usage

let data = {
				username:'hello@gmail.com',
				password:'19578*Adef',
				confirm:'19578*Adef'
		}

		let value = Validator({
			username:'isEmail',
			password:'same:confirm|min:8'
		});

		console.log(value.passes(data),true);

##usage with style of the promise

let data = {
				username:'hello@gmail.com',
				password:'123456',
				confirm:'123456'
		}

		let value = Validator({
			username:'isEmail',
			password:'same:confirm|min:8|match:"[1-6]"'
		}).then(function(){
			console.log(' ','good is working');
		}).catch(function($errors){
			throw new Error('fail, should expect a result true');
		});

		value.eval(data);

Note: The match is wrapper quotes because is need to compiler

You can send error messages to your clients mediating the variable that receives the failure capture function, in this way:

let data = {
				username:'hello@gmail.com',
				password:'123456',
				confirm:'123456'
		}

let value = Validator({
			username:'isEmail',
			password:'same:confirm|min:8|match:"[1-6]"'
}).then(function(){
			console.log(' ','good is working');
}).catch(function($errors){
			if(errors.username)
				throw new Error("the username should is a Email");
			if(errors.password)
				throw new Error("Make sure your password is more than 8 characters");
});

value.eval(data);

##Your function validator

Full-validator contains all the functions of the node validator module. And it is possible to extend your validation systems with your own functions using the extend method.

let data = {
				username:'hello@gmail.com',
				password:'123456',
				confirm:'123456'
		}

		let value = Validator({
			username:'isEmail',
			password:'same:confirm|min:8|match:"[1-6]"'
		}).then(function(){
			console.log(' ','good is working');
		}).catch(function(){
			throw new Error('fail, should expect a result true');
		}).extend('myfunc', function(field,arg){
			// todo  here your implements
			//the `this` context contains the api and other field to make your function
		});

		value.eval(data);

Extended functions can access all the fields of the objective object to make comparisons. Consider the code, of a function finite, by default in the core of the application.

function (string, compare) {
	return string === this.targets [compare];
}

This function receives the name of a field that must be in the objective object, and compares it to see if they are identical, if it is true or false as it is; This basic function is useful for registering users and validating password confirmations.

Extended functions can also access other functions already defined by the kernel or the validator library, to reuse code and make rules that further abstract the validation logic. Consider the min rule. This rule does not exist in the node validator library, but this isLength considers the following code:

function (string, num) {
	return this.helpers.isLength (string, {min: num});
}

The helpers property provides all the methods (which are not few) ofvalidator and combines them with the core libraries, as some of these methods, require additional configuration, you can export an extended function, which configures methods, and that takes into account an argument. As you can see in the previous case.

##Dot Notation

Full-validator supports dot-notation to access very complex collections and evaluate objects within other objects. See an example of the user record http path with express:

const express = require('express');
const bodyParser = require('body-parse');
const validator = require('full-validator');
const session = require('express-session')

var app = express();


app.use(bodyParser());
app.set('trust proxy', 1) // trust first proxy
app.use(session({
  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: true,
  cookie: { secure: true }
}));

app.post('/login/singup/:token', function(req,res,next){

	var valid = validator({
		'body.username': 'isEmail',
		'body.password': 'confirm|min:8|isFullWidth|isHalfWidth',
		'params.token' : 'isHash',
		ip:'isIP'
	});

	valid.then(function(){
		//register user here
	});

	valid.catch(function(error){

		req.session.loginErrors.have = true;
		req.session.loginErrors = errors;
		res.redirect('/login/singin');

	});

	valid.eval();

});
1.5.4

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.9

6 years ago

1.2.9

6 years ago

1.2.8

6 years ago

1.2.7

6 years ago