Introduction

Smooth Validator offers many pre-defined validation helpers that you can use directly inside your javascript (plain, frontend, or backend). These helpers provide standard validation rules. Every time a validation fails, an error is added to the object's errors collection, which is associated with the validated attribute.

Each helper accepts an arbitrary number of attribute names, so you can add the same kind of validation to several attributes with a single line of code.

Installation

We can easily download and install the latest version 4.0.0 via npm.

Command:

npm install smooth-validator

Example

Here is an example of how we can use validation for Registration API. We assigned a couple of rules to the signup user object and passed it to the parser method,
it returns a method that will call it validator, now whenever we want to verify data we need to pass it to the validator method

const parser = require("smooth-validator");
//Sample signup validation
var rules = {
  email : 'required|email|min:5|max:100',
  password: 'required|min:6|max:30',
  confirm_password: 'required|same:password'
}
var validator = parser(rules) //parser returns validator, which can be used on data object

var data = {
  email: 'gisueinc@gmail.com',
  password: 'strong',
  confirm_password: 'strong'
}
console.log(validator(data))
//OUTPUT: { message: 'validation passed', errors: [] }

Message

Default messages can be replace with custom messages, pass object with messages and some params can be used in message like :variable, :value, :first_value, :second_value, :index, :type.
In the case of a nested array, error at index can be found by :zero, :one, :two, :three...

rules = {
  '*emailTo': ["email"],
  created_at: 'after:tomorrow'
}

var validate = parser(rules, {
  message: {
    'emailTo[].email': ':variable failed :type for :value at index :zero',
    'created_at.after' : ':variable is not after :first_value'
  }
})

data = {
  emailTo: ["gmail.com"],
  created_at: new Date()
}
var res = validate(data);
console.log(res.message)
/* Output: 
{
  message: 'emailTo failed email for gmail.com at index 0',
  errors: [
    {
      key: 'emailTo[]',
      message: 'emailTo failed email for gmail.com at index 0'
    },
    { key: 'created_at', message: 'created_at is not after tomorrow' }
  ]
} */

General

These Validators are applicable for all kinds of data types.

Required

Applicable for Boolean, String, Integer and Date

This validates that the value is not the data type's default zero value. For numbers ensures the value is not zero. For strings ensures the value is not "". For array, object, date value is not nil or empty.

Usage: show example hide example

name: 'required'

Error Message:

:variable is required

Example:

var validator = parser({ id: 'required' })

var res = validator({ id: -24.6778 });
console.log( res.message) //validation passed

var res = validator({});
console.log( res.message) //id is required

Applicable for Array and Object

Use * (atrick) before the name to ensure the value is not empty.

Usage: show example hide example

'*near': ["email"]

Error Message:

:variable is required

Example:

var validator = parser({
  '*address': {
    '*near': ["max:12|uppercase"],
  }
})

var res = validator({
  address: {
    near: ["BSECTOR", "DSECTOR"],
  }
});
console.log( res.message) //validation passed

Required If

If the variable is required depends on the value of other variables, at that time this validation can be used.

*Note: only works with string and integer value comparison of other variable

Usage: show example hide example

due_date: 'required_if:status,active'

Error Message:

:variable is required

Example:

// here status is other variable and if status is active then due_date is required
var validator = parser({ due_date: 'required_if:status,active' })

var res = validator({ due_date: null, status: 'inactive' });
console.log( res.message) //validation passed

var res = validator({ due_date: null, status: 'active' })
console.log( res.message) //due_date is required

Same

If you want variables to have the same value you can use `same` (like confirm_passowrd).

*Note: Does not work with array or object

Usage: show example hide example

confirm_passowrd: 'same:password'

Error Message:

:variable is not equal to :first_value

Example:

var validator = parser({ confirm_passowrd: 'same:password' })

var res = validator({ password : 'strong', confirm_passowrd: 'strong' });
console.log( res.message) //validation passed

var res = validator({ password : 'strong', confirm_passowrd: 'week' });
console.log( res.message) //confirm_passowrd is not equal to password

Integer

Various validation for integer data types

Numeric

This validates that a string value contains a basic numeric value. for integers or floats, it returns true.

Usage: show example hide example

numb: 'numeric'

Error Message:

:variable is not number

Example:

var validator = parser({ num: 'numeric' })

var res = validator({ num: '-24.6778' });
console.log( res.message) //validation passed

var res = validator({ num: '-24. 6778'});
console.log( res.message) //num is not number

Between

The field under validation must have a size between the given min and max.

Usage: show example hide example

numb: 'between:10,30'

Error Message:

:variable should be greater than :first_value and less than :second_value

Example:

var validator = parser({ num: 'between:10,30' })

var res = validator({ num: 10 });
console.log( res.message) //validation passed

var res = validator({ num: 9 });
console.log( res.message) //num should be greater than 10 and less than 30

Greater Than

This will ensure that the value is greater than the parameter given.

Usage: show example hide example

num: 'gt:marks'

Error Message:

:variable should be greater than :first_value

Example:

var validator = parser({ num: 'gt:marks' })
var res = validator({ num: 10.1, marks: 10 });
console.log( res.message) //validation passed

var validator = parser({ num: 'gt:10' })
var res = validator({ num: 10 });
console.log( res.message) //num should be greater than 10

Greater Than / Equals

This will ensure that the value is greater than or equal to the parameter given.

Usage: show example hide example

num: 'gte:10'

Error Message:

:variable should be greater than or equals to :first_value

Example:

var validator = parser({ num: 'gte:10' })
var res = validator({ num: 10 });
console.log( res.message) //validation passed

var validator = parser({ num: 'gte:marks' })
var res = validator({ num: 9, marks: 10 });
console.log( res.message) //num should be greater than or equals to marks

Less Than

This will ensure that the value is less than the parameter given.

Usage: show example hide example

num: 'lt:marks'

Error Message:

:variable should be less than :first_value

Example:

var validator = parser({ num: 'lt:marks' })
var res = validator({ num: 9.1, marks: 10 });
console.log( res.message) //validation passed

var validator = parser({ num: 'lt:10' })
var res = validator({ num: 10 });
console.log( res.message) //num should be less than 10

Less Than / Equals

This will ensure that the value is less than or equal to the parameter given.

Usage: show example hide example

num: 'lte:10'

Error Message:

:variable should be less than or equals to :first_value

Example:

var validator = parser({ num: 'lte:10' })
var res = validator({ num: 10 });
console.log( res.message) //validation passed

var validator = parser({ num: 'lte:marks' })
var res = validator({ num: 10.1, marks: 10 });
console.log( res.message) //num should be less than or equals to marks

Odd

Specifies the value must be an odd number.

Usage: show example hide example

num: 'odd'

Error Message:

:variable is not odd number

Example:

var validator = parser({ num: 'odd' })

var res = validator({ num: 9 });
console.log( res.message) //validation passed

var res = validator({ num: 30 });
console.log( res.message) //num is not odd number

Even

Specifies the value must be an even number.

Usage: show example hide example

num: 'even'

Error Message:

:variable is not even number

Example:

var validator = parser({ num: 'even' })

var res = validator({ num: 8 });
console.log( res.message) //validation passed

var res = validator({ num: 31 });
console.log( res.message) //num is not even number

Boolean

Check that given input is boolean

Usage: show example hide example

is_active: 'boolean'

Error Message:

:variable is not boolean

Example:

var validator = parser({ is_active: 'boolean' })

var res = validator({ is_active: 'false' });
console.log( res.message) //validation passed

var res = validator({is_active: 'fdalse'});
console.log( res.message) //is_active is not boolean

Date

Various validation for date data types

Date

Check that given input is date

Usage: show example hide example

datecolumn: 'date'

Error Message:

:variable is not in date format

Example:

var validator = parser({ datecolumn: 'date' })

var res = validator({ datecolumn : new Date() });
console.log( res.message) //validation passed

var res = validator({ datecolumn : 'dmsk' });
console.log( res.message) //datecolumn is not in date format

After

The field under validation must be a value after a given date. Instead of passing a date string, you can specify another field or `today`, `tomorrow`, `yesterday` to compare against the date.

Usage: show example hide example

datecolumn: 'after:yesterday', //today, yesterday also can be used here
datecolumn_two: 'after:datecolumn',
datecolumn_three: 'after:2020-04-25'

Error Message:

:variable is not after :first_value

Example:

var validator = parser({ datecolumn: 'after:yesterday' })
var res = validator({ datecolumn : new Date() });
console.log( res.message) //validation passed

var validator = parser({ datecolumn: 'after:created_at' })
var res = validator({ datecolumn : '2022-05-15', created_at: '2022-05-14' });
console.log( res.message) //validation passed

var validator = parser({ datecolumn: 'after:2022-05-14' })
var res = validator({ datecolumn : '2022-05-15' });
console.log( res.message) //validation passed

var validator = parser({ datecolumn: 'after:tomorrow' })
var res = validator({ datecolumn : new Date() });
console.log( res.message) //datecolumn is not after tomorrow

var validator = parser({ datecolumn: 'after:2022-09-10' })
var res = validator({ datecolumn : '2022-09-10' });
console.log( res.message) //datecolumn is not after 2022-09-10

var validator = parser({ datecolumn: 'after:created_at' })
var res = validator({ datecolumn : '2022-09-10', created_at: '2022-09-10' });
console.log( res.message) //datecolumn is not after created_at

After or Equals

The field under validation must be a value after or equal to the given date. Instead of passing a date string, you can specify another field or `today`, `tomorrow`, `yesterday` to compare against the date.

Usage: show example hide example

datecolumn: 'after_or_equals:yesterday', //today, yesterday also can be used here
datecolumn_two: 'after_or_equals:datecolumn',
datecolumn_three: 'after_or_equals:2020-04-25'

Error Message:

:variable is not after or equals to :first_value

Example:

var validator = parser({ datecolumn: 'after_or_equals:yesterday' })
var res = validator({ datecolumn : new Date() });
console.log( res.message) //validation passed

var validator = parser({ datecolumn: 'after_or_equals:today' })
var res = validator({ datecolumn : new Date() });
console.log( res.message) //validation passed

var validator = parser({ datecolumn: 'after_or_equals:created_at' })
var res = validator({ datecolumn : '2022-05-15', created_at: '2022-05-14' });
console.log( res.message) //validation passed"
var res = validator({ datecolumn : '2022-05-15', created_at: '2022-05-15' });
console.log( res.message) //validation passed

var validator = parser({ datecolumn: 'after_or_equals:2022-05-14' })
var res = validator({ datecolumn : '2022-05-15' });
console.log( res.message) //validation passed

var validator = parser({ datecolumn: 'after_or_equals:tomorrow' })
var res = validator({ datecolumn : new Date() });
console.log( res.message) //datecolumn is not after or equals to tomorrow

var validator = parser({ datecolumn: 'after_or_equals:2022-09-10' })
var res = validator({ datecolumn : '2022-09-09' });
console.log( res.message) //datecolumn is not after or equals to 2022-09-10

var validator = parser({ datecolumn: 'after_or_equals:created_at' })
var res = validator({ datecolumn : '2022-09-09', created_at: '2022-09-10' });
console.log( res.message) //datecolumn is not after or equals to created_at

Before

The field under validation must be a value before a given date. Instead of passing a date string, you can specify another field or `today`, `tomorrow`, `yesterday` to compare against the date.

Usage: show example hide example

datecolumn: 'before:yesterday', //today, yesterday also can be used here
datecolumn_two: 'before:datecolumn',
datecolumn_three: 'before:2020-04-25'

Error Message:

:variable is not before :first_value

Example:

var validator = parser({ datecolumn: 'before:tomorrow' })
var res = validator({ datecolumn : new Date() });
console.log( res.message) //validation passed

var validator = parser({ datecolumn: 'before:created_at' })
var res = validator({ datecolumn : '2022-05-15', created_at: '2022-05-16' });
console.log( res.message) //validation passed

var validator = parser({ datecolumn: 'before:2022-05-14' })
var res = validator({ datecolumn : '2022-05-13' });
console.log( res.message) //validation passed

var validator = parser({ datecolumn: 'before:yesterday' })
var res = validator({ datecolumn : new Date() });
console.log( res.message) //datecolumn is not before yesterday

var validator = parser({ datecolumn: 'before:2022-09-10' })
var res = validator({ datecolumn : '2022-09-10' });
console.log( res.message) //datecolumn is not before 2022-09-10

var validator = parser({ datecolumn: 'before:created_at' })
var res = validator({ datecolumn : '2022-09-10', created_at: '2022-09-10' });
console.log( res.message) //datecolumn is not before created_at

Before or Equals

The field under validation must be a value before or equal to the given date. Instead of passing a date string, you can specify another field or `today`, `tomorrow`, `yesterday` to compare against the date.

Usage: show example hide example

datecolumn: 'before_or_equals:yesterday', //today, yesterday also can be used here
datecolumn_two: 'before_or_equals:datecolumn',
datecolumn_three: 'before_or_equals:2020-04-25'

Error Message:

:variable is not before or equals to :first_value

Example:

var validator = parser({ datecolumn: 'before_or_equals:tomorrow' })
var res = validator({ datecolumn : new Date() });
console.log( res.message) //validation passed

var validator = parser({ datecolumn: 'before_or_equals:today' })
var res = validator({ datecolumn : new Date() });
console.log( res.message) //validation passed

var validator = parser({ datecolumn: 'before_or_equals:created_at' })
var res = validator({ datecolumn : '2022-05-14', created_at: '2022-05-14' });
console.log( res.message) //validation passed"
var res = validator({ datecolumn : '2022-05-14', created_at: '2022-05-15' });
console.log( res.message) //validation passed

var validator = parser({ datecolumn: 'before_or_equals:2022-05-16' })
var res = validator({ datecolumn : '2022-05-15' });
console.log( res.message) //validation passed

var validator = parser({ datecolumn: 'before_or_equals:yesterday' })
var res = validator({ datecolumn : new Date() });
console.log( res.message) //datecolumn is not before or equals to yesterday

var validator = parser({ datecolumn: 'before_or_equals:2022-09-10' })
var res = validator({ datecolumn : '2022-09-11' });
console.log( res.message) //datecolumn is not before or equals to 2022-09-10

var validator = parser({ datecolumn: 'before_or_equals:created_at' })
var res = validator({ datecolumn : '2022-09-11', created_at: '2022-09-10' });
console.log( res.message) //datecolumn is not before or equals to created_at

String

Various validation for string data types

Min

For strings, it checks that the string length is at least that number of characters.

Usage: show example hide example

password: 'min:6'

Error Message:

:variable's length is less than :first_value

Example:

var validator = parser({ password: 'min:6' })
var res = validator({ password : 'passing' });
console.log( res.message) //validation passed

var validator = parser({ password: 'min:8' })
var res = validator({password : 'passing'});
console.log( res.message) //password's length is less than 8

Max

For strings, it checks that the string length is at most that number of characters.

Usage: show example hide example

passport_number: 'max:20'

Error Message:

:variable's length is greater than :first_value

Example:

var validator = parser({ password: 'max:8' })
var res = validator({ password : 'passing' });
console.log( res.message) //validation passed

var validator = parser({ password: 'max:6' })
var res = validator({password : 'passing'});
console.log( res.message) //password's length is greater than 6

Size

For strings, it checks that the string length is exactly that number of characters. For slices, arrays, and maps, validates the number of items.

Usage: show example hide example

phone_number: 'size:10'

Error Message:

:variable's length not equal to :first_value

Example:

var validator = parser({ password: 'size:7' })
var res = validator({ password : 'passing' });
console.log( res.message) //validation passed

var validator = parser({ password: 'size:6' })
var res = validator({password : 'passing'});
console.log( res.message) //password's length not equal to 6

In

For strings, `in` will ensure that the value is one of the values in the parameter. The parameter should be a list of values separated by comma `,`. Values must be strings.

Usage: show example hide example

status: 'in:active,disable'

Error Message:

:variable's value is not in array

Example:

var validator = parser({ status: 'in:active,disable' })

var res = validator({ status : 'active' });
console.log( res.message) //validation passed

var res = validator({status : 'on'});
console.log( res.message) //status's value is not in array

Not In

For strings, `not_in` will ensure that the value is not one of the values in the parameter. The parameter should be a list of values separated by comma `,`. Values must be strings.

Usage: show example hide example

status: 'not_in:inactive,delete'

Error Message:

:variable's value is in invalid array

Example:

var validator = parser({ status: 'not_in:inactive,delete' })

var res = validator({ status : 'active' });
console.log( res.message) //validation passed

var res = validator({ status : 'inactive' });
console.log( res.message) //status's value is in invalid array

Email

This validates that a string value contains a valid email.

Usage: show example hide example

email_address: 'email'

Error Message:

:variable is invalid

Example:

var validator = parser({ email: 'email' })

var res = validator({ email : 'gisueinc@gmail.com' });
console.log( res.message) //validation passed

var res = validator({ email : 'dmsk' });
console.log( res.message) //email is invalid

Alphabetic

This validates that a string value contains ASCII alpha characters only, as well as dashes, spaces and underscores.

Usage: show example hide example

:variable does not match alpha

Error Message:

name: 'alpha',
title: 'alpha:dashes,spaces,underscores'//any of this extra params can be added

Example:

var validator = parser({ name: 'alpha' })
var res = validator({ name : 'bhagyesh' });
console.log( res.message) //validation passed

var validator = parser({ name: 'alpha:dash,space,underscope' })
var res = validator({ name : 'bhagy _e-sh' });
console.log( res.message) //validation passed

var validator = parser({ name: 'alpha' })
var res = validator({ name : 'bhagy _e-sh10' });
console.log( res.message) //name does not match alpha

Alpha-Numeric

The field under validation must be entirely alpha-numeric characters., as well as dashes, spaces and underscores.

Usage: show example hide example

name: 'alphanumeric',
title: 'alphanumeric:dashes,spaces,underscores'//any of this extra params can be added

Error Message:

:variable does not match alphanumeric

Example:

var validator = parser({ name: 'alphanumeric' })
var res = validator({ name : 'bhagyesh10' });
console.log( res.message) //validation passed

var validator = parser({ name: 'alphanumeric:space,dash,underscope' })
var res = validator({ name : 'bhagy _e-sh10' });
console.log( res.message) //validation passed

var validator = parser({ name: 'alphanumeric' })
var res = validator({ name : 'bhagy _e-sh@10' });
console.log( res.message) //name does not match alphanumeric

Uppercase

The field under validation must be entirely uppercase characters, as well as dashes, spaces and underscores.

Usage: show example hide example

name: 'uppercase',
title: 'uppercase:dashes,spaces,underscores'//any of this extra params can be added

Error Message:

:variable does not match uppercase

Example:

var validator = parser({ name: 'uppercase' })
var res = validator({ name : 'BHAGYESH' });
console.log( res.message) //validation passed

var validator = parser({ name: 'uppercase:dash,underscope,space' })
var res = validator({ name : 'BHAGY _E-SH' });
console.log( res.message) //validation passed

var validator = parser({ name: 'uppercase' })
var res = validator({ name : 'bhagy ESH' });
console.log( res.message) //name does not match uppercase

Lowercase

The field under validation must be entirely uppercase characters, as well as dashes, spaces and underscores.

Usage: show example hide example

name: 'lowercase',
title: 'lowercase:dashes,spaces,underscores'//any of this extra params can be added

Error Message:

:variable does not match lowercase

Example:

var validator = parser({ name: 'lowercase' })
var res = validator({ name : 'bhagyesh' });
console.log( res.message) //validation passed

var validator = parser({ name: 'lowercase:dash,underscope,space' })
var res = validator({ name : 'bhagy _e-sh' });
console.log( res.message) //validation passed

var validator = parser({ name: 'lowercase' })
var res = validator({ name : 'bhagy _e-sh10' });
console.log( res.message) //name does not match lowercase

Starts With

This validates that a string value starts with the supplied string value.

Usage: show example hide example

status: 'starts_with:inactive,delete'

Error Message:

:variable does not starts with :first_value

Example:

var validator = parser({ status: 'starts_with:inactive,delete' })

var res = validator({ status : 'inactive-world' });
console.log( res.message) //validation passed

var res = validator({ status : 'active' });
console.log( res.message) //status does not starts with inactive,delete

Starts Not With

This validates that a string value does not starts with the supplied string value.

Usage: show example hide example

status: 'starts_not_with:inactive,delete'

Error Message:

:variable does starts with :first_value

Example:

var validator = parser({ status: 'starts_not_with:inactive,delete' })

var res = validator({ status : 'active' });
console.log( res.message) //validation passed

var res = validator({ status : 'delete' });
console.log( res.message) //status does starts with delete

Ends With

This validates that a string value ends with the supplied string value.

Usage: show example hide example

status: 'ends_with:inactive,delete'

Error Message:

:variable does not ends with :first_value

Example:

var validator = parser({ status: 'ends_with:inactive,delete' })

var res = validator({ status : 'all-delete' });
console.log( res.message) //validation passed

var res = validator({ status : 'all' });
console.log( res.message) //status does not ends with inactive,delete

Ends Not With

This validates that a string value does not ends with the supplied string value.

Usage: show example hide example

status: 'ends_not_with:inactive,delete'

Error Message:

:variable does ends with ':first_value'

Example:

var validator = parser({ status: 'ends_not_with:inactive,delete' })

var res = validator({ status : 'status-active' });
console.log( res.message) //validation passed

var res = validator({ status : 'isdelete' });
console.log( res.message) //status does ends with 'delete'

Contains

This validates that a string value contains the substring value, multiple values can be provided.

Usage: show example hide example

status: 'contains:in,not'

Error Message:

:variable does not contains ':first_value'

Example:

var validator = parser({ status: 'contains:in,not' })

var res = validator({ status : 'inot' });
console.log( res.message) //validation passed

var res = validator({ status : 'nothng' });
console.log( res.message) //status does not contains 'in'

Contains Any

This validates that a string value contains any in the substring value, multiple values can be provided.

Usage: show example hide example

status: 'contains_any:in,not'

Error Message:

:variable does not contains any of :first_value

Example:

var validator = parser({ status: 'contains_any:in,not' })

var res = validator({ status : 'note' });
console.log( res.message) //validation passed

var res = validator({ status : 'betweenvalue' });
console.log( res.message) //status does not contains any of in,not

Array

This tells the validator to get into a array and validate its items. Multidimensional nesting is also supported.

Usage: show example hide example

'*emailTo': ["email"]

Example:

  var validator = parser({ '*emailTo': ["email"] })
  
  var res = validator({ emailTo: ["bsp@gmail.com"] });
  
  console.log( res.message) //validation passed

Object

If you want to validate items in Object, use this.

Usage: show example hide example

'address': {
  'city': "required",
  'pincode': "required",
}

Example:

var validator = parser({
  name: 'required'
  'address': {
    'colony': "required",
    'city': "required",
    'pincode': "required",
  }
})

var res = validator({
  name: 'bhagyesh',
  address: {
    colony: "b-sector",
    city: "pune",
    pincode: 412115
  }
});

console.log( res.message) //validation passed