Package Exports
- node-input-validator
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (node-input-validator) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
NIV (Node Input Validator)
NIV (Node Input Validator) is a validation library for node.js. You can also extend library to add custom rules.
Note: For use case of any rule, please check test cases, If you have any doubt or confusion with documentation or regarding rule behaviour.
Installation
npm i node-input-validator@v5Features
- typescript compatible
- large collection of rules
- add your own custom rules
- supports nested inputs
- declare rules as strings or array
- post validation rules
- modify or add new messages in your own language
- change attribute names globally or locally
- current supported languages: English, Persian(farsi)
Usage
- basic
- koa middleware
- with express
- object validation examples
- array validation examples
- add/modify messages
- change default language examples
- toggle multiple errors
- change attribute names in messages examples
- add custom rules
- rules
Basic Example
js
const { Validator, Rules } = require('node-input-validator');
const v = new Validator(
{ name: '' },
{ name: [Rules.required(), Rules.alpha()] },
);
v.validate().then(function (passed) {
console.log(passed);
console.log(v.errors);
});async/await
const { Validator, Rules } = require('node-input-validator');
const v = new Validator(
{ name: '' },
{ name: [Rules.required(), Rules.alpha()] },
);
const passed = await v.validate()
console.log(passed);
console.log(v.errors);laravel like rules
const { Validator, Rules } = require('node-input-validator');
const v = new Validator(
{ name: '' },
{ name: 'required|alpha' },
);
const passed = await v.validate()
console.log(passed);
console.log(v.errors);ts
import { Validator, Rules } from 'node-input-validator';
const v: Validator = new Validator(
{ name: '' },
{ name: [Rules.required()] },
);
const passed: boolean = await v.validate();
console.log(passed);
console.log(v.errors);For Koa2
Attach koa middleware
const niv = require('node-input-validator');
// keep this under your error handler
app.use(niv.koa());Then in controller
// if validation fails, this will auto abort request with status code 422 and errors in body
await ctx.validate({
name: 'required|maxLength:50',
username: 'required|maxLength:15',
email: 'required|email',
password: 'required'
});
// validation passes
// do some codeWith custom inputs
// if validation fails, this will auto abort request with status code 422 and errors in body
await ctx.validate({
name: 'required|maxLength:50',
username: 'required|maxLength:15',
email: 'required|email',
password: 'required'
}, ctx.request.body);
// validation passes
// do some codeWith custom inputs and custom messages
// if validation fails, this will auto abort request with status code 422 and errors in body
await ctx.validate({
name: 'required|maxLength:50',
username: 'required|maxLength:15',
email: 'required|email',
password: 'required'
}, ctx.request.body, { email: 'E-mail is required' });
// validation passes
// do some codeIn case you wants control over validator, Then use
// if validation fails, this will auto abort request with status code 422 and errors in body
const v = await ctx.validator(ctx.request.body, {
name: 'required|maxLength:50',
username: 'required|maxLength:15',
email: 'required|email',
password: 'required'
});
// in case validation fails
if (v.fails()) {
ctx.status = 422;
ctx.body = v.errors;
return;
}
// do some codewith express
const { Validator } = require('node-input-validator');
app.post('login', function (req, res) {
const v = new Validator(req.body, {
email: 'required|email',
password: 'required'
});
v.validate().then((matched) => {
if (!matched) {
res.status(422).send(v.errors);
}
});
});Objects Validation
Example 1
const v = new Validator(
{
product: {
id: '1',
name: '',
price: '',
active: 'yes',
}
},
{
'product': 'required|object',
'product.id': 'required|integer',
'product.name': 'required',
'product.price': 'required|integer',
'product.active': 'required|integer'
},
);
const matched = await v.validate();Array Validation
Example 1
let v = new Validator(
{
roles: ['admin', 'manager', 'member']
},
{
'roles': 'required|array',
'roles.*': 'required|string'
},
);
let matched = await v.check();Example 2
let v = new Validator(
{
plans: [
{ price: '25', title: 'OK' },
{ price: '', title: '' },
{ price: '30' },
{ price: '', title: 'Title' }
]
},
{
'plans': 'required|array',
'plans.*.price': 'required|integer',
'plans.*.title': 'required'
},
);
let matched = await v.check();Rules
You can declare rules in string or in array
accepted
The field under validation must be yes, on, 1, or true.
new Validator(
inputs,
{ terms: 'accepted' },
);Customize: will only allow 1 ("1" should be string)
new Validator(
inputs,
{ terms: 'accepted:1' },
);in array example
new Validator(
inputs,
{ terms: [Rules.accepted()] },
);Customise in array style
new Validator(
inputs,
{ terms: [Rules.accepted(['1'])] },
);