5.0.0-rc8 • Published 5 years ago

node-input-validator v5.0.0-rc8

Weekly downloads
7,689
License
ISC
Repository
github
Last release
5 years ago

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@v5

Features

  • 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 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 code

With 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 code

With 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 code

In 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 code

with 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'])] },
);
5.0.0-beta.8

2 years ago

5.0.0-beta.6

3 years ago

5.0.0-beta.7

3 years ago

4.5.1

3 years ago

4.5.0

4 years ago

5.0.0-beta.5

4 years ago

5.0.0-beta.4

4 years ago

4.4.1

4 years ago

5.0.0-beta.3

4 years ago

4.4.0

4 years ago

4.3.3

4 years ago

4.3.2

4 years ago

4.3.1

4 years ago

5.0.0-beta.2

4 years ago

4.3.0

4 years ago

5.0.0-beta.1

4 years ago

5.0.0-beta.0

4 years ago

5.0.0-rc11

4 years ago

5.0.0-rc10

4 years ago

5.0.0-rc9

5 years ago

5.0.0-rc8

5 years ago

5.0.0-rc7

5 years ago

5.0.0-rc5

5 years ago

5.0.0-rc6

5 years ago

5.0.0-rc4

5 years ago

5.0.0-rc3

5 years ago

5.0.0-rc2

5 years ago

5.0.0-rc1

5 years ago

4.2.2-rc1

5 years ago

4.2.1

5 years ago

4.2.0

5 years ago

4.2.0-rc2

6 years ago

4.2.0-rc1

6 years ago

4.1.0

6 years ago

4.1.0-rc1

6 years ago

3.8.0

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

4.0.0-rc6

6 years ago

4.0.0-rc5

6 years ago

3.7.3

6 years ago

4.0.0-rc4

6 years ago

4.0.0-rc3

6 years ago

4.0.0-rc2

6 years ago

4.0.0-rc1

6 years ago

3.7.2

6 years ago

3.7.1

6 years ago

3.7.0

6 years ago

3.7.0-rc2

6 years ago

3.7.0-rc1

6 years ago

3.6.4

6 years ago

3.6.3

6 years ago

3.6.2

6 years ago

3.6.1

6 years ago

3.6.0

6 years ago

3.6.0-rc2

6 years ago

3.6.0-rc1

6 years ago

3.5.0

6 years ago

3.4.2

6 years ago

3.4.1

6 years ago

3.4.0

6 years ago

3.4.0-rc1

6 years ago

3.3.0

6 years ago

3.3.0-rc3

6 years ago

3.3.0-rc2

6 years ago

3.3.0-rc1

6 years ago

3.2.0

6 years ago

3.2.0-rc3

6 years ago

3.2.0-rc2

6 years ago

3.2.0-rc1

6 years ago

3.1.0

6 years ago

3.1.0-rc3

6 years ago

3.1.0-rc2

6 years ago

3.0.0-rc1

6 years ago

2.3.4

6 years ago

2.3.3

7 years ago

2.3.2

7 years ago

2.3.1

7 years ago

2.3.0

7 years ago

2.2.0

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.3

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.3

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.0.9

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago