# abstract-validator

> Simple js data validator to check form inputs.

Latest version **1.1.0** (published 2019-03-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install abstract-validator
pnpm add abstract-validator
yarn add abstract-validator
bun add abstract-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.1.0 |
| Published | 2019-03-03 |
| First published | 2019-01-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 30.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | mahdrentys |
| Maintainers | mahdrentys |
| Keywords | form, validator, abstract, data, inputs, object |

## Links

- npm: https://www.npmjs.com/package/abstract-validator
- Repository: https://github.com/Mahdrentys/JS-Abstract-Validator
- Homepage: https://github.com/Mahdrentys/JS-Abstract-Validator#readme
- Issues: https://github.com/Mahdrentys/JS-Abstract-Validator/issues
- npm.io page: https://npm.io/package/abstract-validator

## Dependencies (1)

- [deep-eql](https://npm.io/package/deep-eql.md) ^4.0.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2019-03-03
- 1.0.1 — 2019-03-03
- 1.0.0 — 2019-01-05

## README

# Abstract Validator

Abstract Validator is a javascript library to help you to validate data objects, for html forms for example, which can work server-side or client-side.

## Installation

Make sure that you have npm installed, and run:

```
npm install abstract-validator
```

## Usage

Validator has a method called `validate` which takes as arguments the inputs (as an object) and the rules, and which returns the errors as an object.

For example, here is the validation of a comment post:

```js
const Validator = require('abstract-validator')

let rules =
{
    pseudo: ['required', 'string', {maxChars: 15}],
    email: ['required', 'email'],
    comment: ['required', 'string', {maxChars: 1500}]
}

let inputs =
{
    pseudo: 'John Doe',
    email: 'john.doe@gmail.com',
    comment: 'Blablabla...'
}

let errors = Validator.validate(inputs, rules)
errors == {pseudo: null, email: null, comment: null} // true

inputs =
{
    pseudo: '   '
    email: 'john.doe',
    comment: 'Imagine that there are more than 1500 characters here'
}

errors = Validator.validate(inputs, rules)
errors ==
{
    pseudo: 'This field is required.',
    email: 'This field must be a valid email.',
    comment: 'This field must be less than or equal to 1500.'
} // true
```

So, `Validator.validate` returns an object of errors with the field names as keys and the first error message as value.

Some rules are simply strings, whereas some rules have values (like `maxChars`) so they are objects with the rule name as key and the value as value (like `{maxChars: 15}`).

Here are the different available rules:

- `required`: Check if the field exists and if it's not empty.

- `nullable`: Authorize the field to be a `null` value.

- `string`: Check if the field is a string.

- `int`: Check if the field is an integer.

- `number`: Check if the field is a number.

- `boolean`: Check if the field is a boolean.

- `array`: Check if the field is an array.

- `object`: Check if the field is an object.

- `equalTo` (has whatever as value): Check if the field equals some value.

- `match` (has a RegExp as value): Check if the field match some regular expression.

- `minChars` (has a number as value): Check if the field has a least a certain number of characters.

- `maxChars` (has a number as value): Check if the field has at most a certain number of characters.

- `min` (has a number as value): Check if the field is greater than or equal to a certain number.

- `max` (has a number as value): Check if the field is less than or equal to a certain number.

- `email`: Check if the field is a valid email.

## Language

By default, the messages are in English. You can change the language with the method `Validator.setLocale`:

```js
const Validator = require('abstract-validator')
Validator.setLocale('en')
```

Currently, there is only two languages available: English (`en`) and French (`fr`).

You can check which locale is currently setted with the method `Validator.getLocale`.

You can add a new language by passing a object of messages for each rules, like this:

```js
const Validator = require('abstract-validator')

Validator.addLocale('en', // To replace with your language
{
    required: 'This field is required.',
    string: 'This field must be text.',
    int: 'This field must be an integer.',
    number: 'This field must be an number.',
    boolean: 'This field must be a boolean.',
    array: 'This field must be an array.',
    object: 'This field must be an object.',
    equalTo: 'This field must be equal to "%val%".', // %val% will be replaced by the rule value
    match: 'This field is not valid.',
    minChars: 'This field must have at least %val% characters.', // %val% will be replaced by the rule value
    maxChars: 'This field must have at most %val% characters.', // %val% will be replaced by the rule value
    min: 'This field must be greater than or equal to %val%.', // %val% will be replaced by the rule value
    max: 'This field must be less than or equal to %val%.', // %val% will be replaced by the rule value
    email: 'This field must be a valid email.'
})
```

Or you can contribute to the project and add the translation to the repo, by creating a json file in the `messages/` folder and adding `addLocale('aa', require('./messages/aa.json'))` (where `aa` is the locale) to `abstract-validator.js`.

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