validator-wrapper

A validator.js wrapper to support validation chaining and messages

Usage no npm install needed!

<script type="module">
  import validatorWrapper from 'https://cdn.skypack.dev/validator-wrapper';
</script>

README

Introduction

A Nodejs wrapper of validator.js to support validation chaining and messages

Version 1.0.3

  • Add f as the alias of prepare function
  • Add `x' as the alias of 'eval' function

Features

  • Method chaining
  • Batch validations with messages and additional info (tag)
  • Able to call any validation method of validator.js with options
valid_wrapper.prepare("isInt", { min: 10, max: 99 }).eval('Age must be between 10 and 99')

//use alias
valid_wrapper.f("isInt", { min: 10, max: 99 }).x('Age must be between 10 and 99')

Getting Started

Installation

npm install validator-wrapper

Usage

var valid_wrapper = require('validator-wrapper')

var data = {
    email : 'trietho@gmail.com',
    email2: "bademail",
    firstname: "Triet",
    lastname: "",
    age: 1,
    password: "trietho@gmail.com",
    password2: "123456",
} 

//the default tag is 'danger', set options to change the default tag
valid_wrapper.options({tag: 1}) 

valid_wrapper.pick(data.email)
    .empty('Main email must be not empty') //this message will bot be included in the validation results
    .email('Main email is invalid') //this message will not be included in the validation results
    
    .pickNext(data.email2)
    .email('Second email is invalid')

    .pickNext(data.firstname)
    .empty('First name must be not empty') //this message will not be included in the validation results

    .pickNext(data.lastname)
    .length({min:3}, "Last name must has at least 3 characters")

    .pickNext(data.password)
    .equals(data.email, 'Password must be different from email', 'warning')
    .prepare("isAlpha").eval('Password contains letters a-zA-Z only')
    .diff(data.password2, 'Passwords do not match')
    
    .pickNext(data.age)
    .f("isInt", { min: 10, max: 99 }).x('Age must be between 10 and 99', 2)

    .pickNext(data.notexist)
    .empty('Notexist must be exist :)')

console.log(JSON.stringify(valid_wrapper.messages, null, 4))

/* ouput 

[
    {
        "message": "Second email is invalid",
        "tag": 1
    },
    {
        "message": "Last name must has at least 3 characters",
        "tag": 1
    },
    {
        "message": "Password must be different from email",
        "tag": "warning"
    },
    {
        "message": "Password contains letters a-zA-Z only",
        "tag": 1
    },
    {
        "message": "Passwords do not match",
        "tag": 1
    },
    {
        "message": "Age must be between 10 and 99",
        "tag": 2
    },
    {
        "message": "Notexist must be exist :)",
        "tag": 1
    }
]
*/

API references

functions

  • pick(content): set the first content for validating and clear existing messages
  • pickNext(content): set the next content for validating and keep existing messages
  • length(options, message[, tag]): if not valid, add the message into validation results
  • email(message[,tag]): if not email, add the message into validation results
  • empty(message[, tag]): if empty, add the message into validation results, and ignore other validations until pickNext
  • equals(target, message[, tag])
  • diff(target, message[, tag])
  • custom(condition, message[, tag]): if condition is true, add the message into validation results
  • prepare(method, options): select a method of validatorjs and its options that to be executed in eval method
    • Alias: ffunction
  • eval(message[, tag]): execute the method defined in the prepare function and add the message into validation results if the returned value of validator is false
    • Alias: x function

fields

  • messages contains the validation results
  • validator direct access to validatorjs module