1.1.5 • Published 6 years ago

aj-validator v1.1.5

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

aj-validator

Simple and extensible validator library for node.js.

Installation

ES6

import validator from 'aj-validator'

No ES6

var validator = require('aj-validator');

Basic usage

let is_valid = validator.email('aj@mail.com');
console.log(is_valid); // true


let is_valid = validator.max('some text', 4); // Passing parameters
console.log(is_valid); // false

Validate multiple fields

api.post('/user', (req, res) => {
  
  // validationRules expect to be an object with: 
  // key = field_to_validate
  // value = rules_separate_by_pipe:rule_parameters 
  
  let validationRules = {
    username: 'required|email|min:6',
    name: 'required|min:2',
    description: 'required|max:255'
  };
  
  let is_valid = validator.validate(req.body, validationRules);
  console.log(is_valid); // true|false

  let errors = validator.getErrors(); // Retriving validation errors
  console.log(errors); // {name: ["required"], username: ["Invalid Email provided", "Less than 6 characters are not allowed"]}

});

Create custom validators

// customValidator expect to be an object with: 
// name = Validator name
// message = Message to display in case validation fails
  
let customValidator = {
  name: 'custom',
  message: 'Message to display in case validation fails'
};

// .make() method expect 2 parameters:
// customValidator object {name: '', message: ''}
// fn callback receiving value(string) to validate and parameters(string, number or array)
// Should return true|false

validator.make(customValidator, (val, ruleParams) => val.length >= ruleParams );

let validationRules = {
  username: 'required|custom',
  name: 'required|min:2',
};

let is_valid = validator.validate(req.body, validationRules);
console.log(is_valid); // true|false

Customize messages to display

let customMessages = {
  'required':'Message to be display only on fields if required rule fail',
  'required.username': 'Message to be displayed only on username field if required rule fail',
};

let validationRules = {
  username: 'required|email|min:6',
  name: 'required|min:2'
};

let fieldsToValidate = {
  name: '',
  username: ''
};

let is_valid = validator.validate(fieldsToValidate, validationRules, customMessages);
console.log(is_valid); // true|false

let errors = validator.getErrors(); // Retriving validation errors
console.log(errors); // {name: ["Message to be display only on fields if required rule fail"], 
                     //  username: [Message to be displayed only on username field if required rule fail]}

Validators built-in

ValidatorParameterDescription
requirednoneThe field under validation must be present in the input data and not empty
emailnoneThe field under validation must be formatted as an e-mail address
max      integerThe field under validation must be less than or equal to a maximum value
min      integerThe field under validation must have a minimum value
jsonnoneThe field under validation must be a valid JSON string
urlnoneThe field under validation must be a valid URL
datenoneThe field under validation must be a valid date according to javascript Date object
integernoneThe field under validation must be an integer
1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago