Icontem

Node Input Validator: Validate submitted input values in Node.js

Recommend this page to a friend!
  Info   View files Documentation   View files View files (24)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-10-21 (2 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 25 This week: 12All time: 514 This week: 1Up
Version License JavaScript version Categories
node-input-validator 1.0Freeware5Validation, Node.js, Forms, EcmaScript 6
Description Author

This package can validate submitted input values in Node.js.

It takes a set of rules to apply to the values of submitted inputs for validation.

A callback function is invoked to process invalid inputs passing information on the respective error messages.

The package can be extended to support custom validation rules.

Innovation Award
JavaScript Programming Innovation award nominee
September 2017
Nominee
Vote
Validation library for node.js

Node Input Validator is a validation library for node.js. You can also extend library to add custom rules.

Installation

npm install node-input-validator


Manuel Lemos
Name: Harcharan Singh <contact>
Classes: 4 packages by
Country: India India
Age: 25
All time rank: 11210 in India India
Week rank: 2 Up1 in India India Up
Innovation award
Innovation award
Nominee: 2x

Details

Validation Library

[![NPM version][npm-image]][npm-url] [![build status][travis-image]][travis-url] Coverage Status [![David deps][david-image]][david-url] [![node version][node-image]][node-url]

[travis-image]: https://api.travis-ci.org/artisangang/node-input-validator.svg?branch=master [travis-url]: https://travis-ci.org/artisangang/node-input-validator

[david-image]: https://david-dm.org/artisangang/node-input-validator.svg?style=flat-square [david-url]: https://david-dm.org/artisangang/node-input-validator

[npm-image]: https://img.shields.io/npm/v/node-input-validator.svg?style=flat-square [npm-url]: https://npmjs.org/package/node-input-validator [node-image]: https://img.shields.io/badge/node.js-%3E=_7.6-green.svg?style=flat-square [node-url]: http://nodejs.org/download/

Validation library for node.js

Node Input Validator is a validation library for node.js. You can also extend library to add custom rules.

Installation


Usage

const v = require('node-input-validator');

let r = {}; // first argument for constructor will always be blank object

          // This empty object (i.e. r in this case) will be used in future

let validator = new v(r, {name:''}, {name:'required|minLength:5'});

validator.check().then(function (matched) {

console.log(matched);
console.log(validator.errors);

});


Extending

Validator.messages({

even: 'The value of the field must be even number.',
status: 'Invalid status'

});

Validator.extend('even', async function (field, value) {

if( (parseInt(value) % 2) == 0 ){
	return true;
} 

this.validator.addError(field, 'even');
return false;

});

Validator.extend('status', async function (field, value, args) {

if( args.indexOf(value) >= 0 ){
	return true;
}

this.validator.addError(field, 'status');
return false;

});


Extending/Overriding messages

Validator.messages({

required: 'The :attribute field must not be empty.',

});


validator.rules.validateCustom = async (field, value) => {

    	
if( value === 'yes' || value === 'on' ){
    return true;
}

this.validator.addError(field, 'custom');
return false;

};


For Koa2
Attach koa middleware

const validator = require('node-input-validator'); app.use(validator.koa());

Controller Example

let v = await ctx.validate(ctx.request.body, {

	name:'required|maxLength:50', 
	username:'required|maxLength:15',
	email:'required|email',
	password:'required'
});

let isValid = await v.check();

if (!isValid) {

// return validation errors
ctx.body = v.errors;

}


Rules

You can check test cases for rules.

required  
requiredIf:age,16  
requiredNotIf:age,16  
requiredWith:age  
requiredWithout:age  

accepted  
The field under validation must be yes, on, 1, or true.

alpha  
The field under validation must be entirely alphabetic characters.

alphaDash  
The field under validation may have alpha-numeric characters, as well as dashes and underscores.

alphaNumeric  
The field under validation only contains letters and numbers.

array  
The field under validation must be an array.

ascii  
The field under validation only contains ascii characters.

base64  
The field under validation must be valid base64 encoded string.

between:1,9  
The field under validation must be between provided values.

boolean  
The field under validation must be 0/1, or true/false.

contains  
The field under validation must contains provided seeds.

creditCard  
The field under validation must be valid credit card string.

dateFormat  
The field under validation must match the given date format.

digits  
The field under validation only contains digits.

digitsBetween  
The field under validation must be in between provided digit values.

email  
The field under validation must be formatted as an e-mail address.

equals  
The field under validation must be equal to given value.

in  
The field under validation must exist in the given list of values.

integer  
The field under validation must be an integer.

ip  
The field under validation must be an IP address.

json   
The field under validation must be a valid JSON string.

latLong   
The field under validation must be a valid latitude-longitude coordinate.

max  
The field under validation must be less than given value.

mime  
The file under validation must have a MIME type corresponding to one of the listed extensions.

min   
The field under validation must be greater than given value.

maxLength   
The length of field under validation should be less than given value.

minLength  
The length of field under validation  should be greater than given value.

notIn  
The field under validation must not exist in the given list of values.

numeric  
The field under validation must be numeric.

regex  
The field under validation must match the given regular expression.

same  
The given field must match the field under validation.

size  
The file field  under validation must have a file size matching the given maximum value or should be between file size range.

string  
The field under validation must be string.

url  
The field under validation must be a valid URL.

Post Rules

There is set of rules which can be used to validate constraints of whole input, rather than validity of singular fields.

Usage

const v = require('node-input-validator');

let r = {}; // first argument for constructor will always be blank object

          // This empty object (i.e. r in this case) will be used in future

let validator = new v(r, {name:''}, {'*': 'any:name,surname'});

validator.check().then(function (matched) {

console.log(matched);
console.log(validator.errors);

});


Post validator errors are returned in the `*` key. There is also possibility to add custom function as validator
with help of `addPostRule` method. Function will be called in context of validator object with input as parameter.

const v = require('node-input-validator');

let r = {}; // first argument for constructor will always be blank object

          // This empty object (i.e. r in this case) will be used in future

let v = new Validator(r, {username: 'arnold', password: 'arnold123'}, {});

v.addPostRule(async function(input) {

if (input.password.indexOf(input.username) >= 0) {
    this.validator.addError('password', 'custom', 'Password cannot contain username'); 
}

});


any
Any of the fields must be present in input.

all
All of the fields must be present in input.


 
  Files folder image Files  
File Role Description
Files folder imagelib (4 files, 1 directory)
Files folder imagetest (10 files, 1 directory)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Plain text file index.js Class Class source
Accessible without login Plain text file package-lock.json Data Auxiliary data
Accessible without login Plain text file package.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  lib  
File Role Description
Files folder imagerules (3 files)
  Plain text file messages.js Class Class source
  Plain text file post.js Class Class source
  Plain text file rules.js Class Class source
  Plain text file validator.js Class Class source

  Files folder image Files  /  lib  /  rules  
File Role Description
  Plain text file date.js Class Class source
  Plain text file file.js Class Class source
  Plain text file mix.js Class Class source

  Files folder image Files  /  test  
File Role Description
Files folder imagefiles (2 files)
  Accessible without login Plain text file conditions.js Example Example script
  Accessible without login Plain text file custom-messages.js Example Example script
  Plain text file custom.js Class Class source
  Accessible without login Plain text file date.js Example Example script
  Accessible without login Plain text file file.js Example Example script
  Accessible without login Plain text file multiple-rules.js Example Example script
  Accessible without login Plain text file numbers.js Example Example script
  Accessible without login Plain text file pattern.js Example Example script
  Plain text file post.js Class Class source
  Accessible without login Plain text file required-rules.js Example Example script

  Files folder image Files  /  test  /  files  
File Role Description
  Accessible without login Image file file-big.jpg Data Auxiliary data
  Accessible without login Image file file-small.png Icon Icon image

 Version Control Unique User Downloads Download Rankings  
 100%
Total:25
This week:12
All time:514
This week:1Up