abstract-validator

1.1.0 • Public • Published

Abstract Validator

Abstract Validator is a javascript library to help you to validate data objects, for html forms for example, which can work server-side or client-side.

Installation

Make sure that you have npm installed, and run:

npm install abstract-validator

Usage

Validator has a method called validate which takes as arguments the inputs (as an object) and the rules, and which returns the errors as an object.

For example, here is the validation of a comment post:

const Validator = require('abstract-validator')
 
let rules =
{
    pseudo: ['required', 'string', {maxChars: 15}],
    email: ['required', 'email'],
    comment: ['required', 'string', {maxChars: 1500}]
}
 
let inputs =
{
    pseudo: 'John Doe',
    email: 'john.doe@gmail.com',
    comment: 'Blablabla...'
}
 
let errors = Validator.validate(inputs, rules)
errors == {pseudo: null, email: null, comment: null} // true
 
inputs =
{
    pseudo: '   '
    email: 'john.doe',
    comment: 'Imagine that there are more than 1500 characters here'
}
 
errors = Validator.validate(inputs, rules)
errors ==
{
    pseudo: 'This field is required.',
    email: 'This field must be a valid email.',
    comment: 'This field must be less than or equal to 1500.'
} // true

So, Validator.validate returns an object of errors with the field names as keys and the first error message as value.

Some rules are simply strings, whereas some rules have values (like maxChars) so they are objects with the rule name as key and the value as value (like {maxChars: 15}).

Here are the different available rules:

  • required: Check if the field exists and if it's not empty.

  • nullable: Authorize the field to be a null value.

  • string: Check if the field is a string.

  • int: Check if the field is an integer.

  • number: Check if the field is a number.

  • boolean: Check if the field is a boolean.

  • array: Check if the field is an array.

  • object: Check if the field is an object.

  • equalTo (has whatever as value): Check if the field equals some value.

  • match (has a RegExp as value): Check if the field match some regular expression.

  • minChars (has a number as value): Check if the field has a least a certain number of characters.

  • maxChars (has a number as value): Check if the field has at most a certain number of characters.

  • min (has a number as value): Check if the field is greater than or equal to a certain number.

  • max (has a number as value): Check if the field is less than or equal to a certain number.

  • email: Check if the field is a valid email.

Language

By default, the messages are in English. You can change the language with the method Validator.setLocale:

const Validator = require('abstract-validator')
Validator.setLocale('en')

Currently, there is only two languages available: English (en) and French (fr).

You can check which locale is currently setted with the method Validator.getLocale.

You can add a new language by passing a object of messages for each rules, like this:

const Validator = require('abstract-validator')
 
Validator.addLocale('en', // To replace with your language
{
    required: 'This field is required.',
    string: 'This field must be text.',
    int: 'This field must be an integer.',
    number: 'This field must be an number.',
    boolean: 'This field must be a boolean.',
    array: 'This field must be an array.',
    object: 'This field must be an object.',
    equalTo: 'This field must be equal to "%val%".', // %val% will be replaced by the rule value
    match: 'This field is not valid.',
    minChars: 'This field must have at least %val% characters.', // %val% will be replaced by the rule value
    maxChars: 'This field must have at most %val% characters.', // %val% will be replaced by the rule value
    min: 'This field must be greater than or equal to %val%.', // %val% will be replaced by the rule value
    max: 'This field must be less than or equal to %val%.', // %val% will be replaced by the rule value
    email: 'This field must be a valid email.'
})

Or you can contribute to the project and add the translation to the repo, by creating a json file in the messages/ folder and adding addLocale('aa', require('./messages/aa.json')) (where aa is the locale) to abstract-validator.js.

Package Sidebar

Install

npm i abstract-validator

Weekly Downloads

0

Version

1.1.0

License

MIT

Unpacked Size

31.2 kB

Total Files

15

Last publish

Collaborators

  • mahdrentys