# @vqq/exp-validator

> Validator for expressJS

Latest version **1.0.4** (published 2021-04-11) · ISC license · 0 weekly downloads

## Install

```sh
npm install @vqq/exp-validator
pnpm add @vqq/exp-validator
yarn add @vqq/exp-validator
bun add @vqq/exp-validator
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2021-04-11 |
| First published | 2021-03-30 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 18.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | vanquangqui |
| Keywords | validator, nodejs, express |

## Links

- npm: https://www.npmjs.com/package/@vqq/exp-validator
- Homepage: https://github.com/jackiewen/exp-validator
- npm.io page: https://npm.io/package/@vqq/exp-validator

## Dependencies (4)

- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [express](https://npm.io/package/express.md) ^4.17.1
- [validator](https://npm.io/package/validator.md) ^13.5.2
- [sprintf-js](https://npm.io/package/sprintf-js.md) ^1.1.2

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.0.4 (latest) — 2021-04-11
- 1.0.3 — 2021-04-10
- 1.0.2 — 2021-04-10
- 1.0.1 — 2021-04-07
- 1.0.0 — 2021-03-30

## README

# Exp Validtor
## _Validator for Express_

[![Build Status](https://travis-ci.org/joemccann/dillinger.svg?branch=master)](https://github.com/jackiewen/exp-validator.git)

Exp-Validtor is a wrapper for validator package to use like a Express middleware.
[Here](https://www.npmjs.com/package/validator) 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.

```sh
npm i @vqq/exp-validator
```

Or yarn

```sh
yarn add @vqq/exp-validator
```

## Usage

Require package to main server.
```sh
const expValidator = require('@vqq/exp-validator');
```
Set up locale for validator
```sh
expValidator.locale.readMessages();
```
You can set locale code or set path to your language file by json format.
```sh
expValidator.locale.setLocale('jp').readMessages();
expValidator.locale.setLocale('jp').setPath('path/:locale/filename.json').readMessages();
```
Set your own custom rules by setCustomRules
```sh
expValidator.setCustomRules({
    isTest: function(str, param1, param2) { return str === param1 && param2.check === 'check param 2'; }
});
```
Finally, Use your Exp-Validator like a middleware.
```sh
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
```sh
expValidator.validate({
    email: [
        'isEmail'
    ],
}, {data: {
    email: 'your_email@your.domain'
}});
```
method option (COOKIE, HEADER)
```sh
expValidator.validate({
    email: [
        'isEmail'
    ],
}, {method: 'COOKIE'});
```
messages and attributes options
```sh
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.
```sh
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: 
```sh
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');
});
```

---
_Source: https://npm.io/package/@vqq/exp-validator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
