Git Product home page Git Product logo

exp-validator's Introduction

Exp Validtor

Validator for Express

Build Status

Exp-Validtor is a wrapper for validator package to use like a Express middleware. Here is a list of the validators currently available.

Features

  • Cover all validator rules.
  • Easy to use like a Express middleware.
  • Easy to custom rules.
  • Easy to custom messages and attributes.

Installation

Install package with npm.

npm i @vqq/exp-validator

Or yarn

yarn add @vqq/exp-validator

Usage

Require package to main server.

const expValidator = require('@vqq/exp-validator');

Set up locale for validator

expValidator.locale.readMessages();

You can set locale code or set path to your language file by json format.

expValidator.locale.setLocale('jp').readMessages();
expValidator.locale.setLocale('jp').setPath('path/:locale/filename.json').readMessages();

Set your own custom rules by setCustomRules

expValidator.setCustomRules({
    isTest: function(str, param1, param2) { return str === param1 && param2.check === 'check param 2'; }
});

Finally, Use your Exp-Validator like a middleware.

const app = express();
app.post('/create', expValidator.validate({
    email: [
        'nullable',
        'isEmail',
        {isByteLength: [{min:15, max: 50 }]}
    ],
    test: [
        {isTest: ['check param 1', {check: 'check param 2'}]}
    ]
})
,function (req, res) {
    // Your code here
});

Some options you can change.

Change options by passing a object after rules. data option

expValidator.validate({
    email: [
        'isEmail'
    ],
}, {data: {
    email: '[email protected]'
}});

method option (COOKIE, HEADER)

expValidator.validate({
    email: [
        'isEmail'
    ],
}, {method: 'COOKIE'});

messages and attributes options

expValidator.validate({
    email: [
        'isEmail'
    ],
    test: [
        {isTest: ['check param 1', {check: 'check param 2'}]}
    ]
}, {
    messages: {
        'isEmail': 'The :attribute must be a valid email address.',
        'test.isTest': 'The :attribute must be equal "%(params[0])s" and param 2 must be equal "%(params[1].check)s".',
    },
    attributes: {
        'email': 'Email',
        'test': 'Test Param'
    }
});

If you want to response all error messages for every rule. you can set checkFirst option to false.

expValidator.validate({
    email: [
        'nullable',
        'isEmail',
        {isByteLength: [{min:15, max: 50 }]}
    ]
}, {checkFirst: false})

nullable rule is special rule. It won't check other rules if your field is empty.

Advanced usage

You can use validator by your own way. Like this:

const express = require('express');
const expValidator = require('@vqq/exp-validator');

const app = express();
// expValidator.locale.readMessages();
expValidator.setCustomRules({
    isTest: function(str, param1, param2) { return str === param1 && param2.check === 'check param 2'; }
});
expValidator.validator.setMessages({
    'isEmail': 'The :attribute must be a valid email address.',
    'test.isTest': 'The :attribute must be equal "%(params[0])s" and param 2 must be equal "%(params[1].check)s".',
}).setAttributes({
    'email': 'Email',
    'test': 'Test Param'
});

app.post('/create', function (req, res) {
    const result = expValidator.validator.setData({
        email: 'youremail@domain',
        test: 'check param 2'
    }).setRules({
         email: [
            'isEmail'
        ],
        test: [
            {isTest: ['check param 1', {check: 'check param 2'}]}
        ]
    }).validate();
    if (result) {
        return res.status(422).send(result);
    }
    // Your code here
    return res.status(200).send('success');
});

exp-validator's People

Contributors

jackiewen avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.