1.0.8 • Published 5 years ago

eagle-validator v1.0.8

Weekly downloads
5
License
MIT
Repository
github
Last release
5 years ago

eagle-validator

Build Status Coverage Status

A modern javascript object validation node package available on npmjs.com

Basic usage

a simple middleware for you

import Check from 'eagle-validator';

export default function validator(req, res, next) {
  const valid = [
    new Check({ firstName: req }).str().req().min(2).max(20).alpha(),
    new Check({ phoneNumber: req }).str().req().min(2).num(),
    new Check({ email: req }).str().req().email(),
  ];
  const invalid = valid.find((e) => e.error);
  if (invalid) return res.status(400).send({ status:400, message:invalid.error });
  return next();
}

this will return simplified messages like:

for required field

firstName field is required

firstName should be a string

firstName should be greaterthan 2

firstName should be lessthan 19

firstName should be alphabetic

Basic abbrevations used

abbrevationsmeaningexample
str()string'hello'
req()required'world'
min()minimum charactersany
max(3)miximum charactersany
number()should be a number1-9
num()only numbers in a string0-9
alpha()should ba alphabets onlyA-Z,a-z
alphaNum()mixture of alpha & numsA-Z,a-z,0-9
bool()should be a booleantrue
email()stictly email addressrswaib@gmail.com