# valite

> Concurrently execute your validators in a simple, practical and light validator engine.

Latest version **0.7.0** (published 2018-11-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install valite
pnpm add valite
yarn add valite
bun add valite
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.7.0 |
| Published | 2018-11-13 |
| First published | 2018-05-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 28.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 20 |
| Author | Vitor Luiz Cavalcanti |
| Maintainers | vitorluizc |
| Keywords | validate, validator, check, checker |

## Links

- npm: https://www.npmjs.com/package/valite
- Repository: https://github.com/VitorLuizC/valite
- Homepage: https://github.com/VitorLuizC/valite#readme
- Issues: https://github.com/VitorLuizC/valite/issues
- npm.io page: https://npm.io/package/valite

## Dependencies (1)

- [get-value](https://npm.io/package/get-value.md) ^3.0.1

## Alternatives

- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads
- [@oridune/validator](https://npm.io/package/@oridune/validator.md) — 16 weekly downloads

## Recent versions

- 0.7.0 (latest) — 2018-11-13
- 0.6.0 — 2018-10-15
- 0.5.0 — 2018-09-21
- 0.4.0 — 2018-08-01
- 0.3.1 — 2018-05-20
- 0.3.0 — 2018-05-20
- 0.2.0 — 2018-05-14
- 0.1.1 — 2018-05-14
- 0.0.0 — 2018-05-13

## README

# valite

[![Build Status](https://travis-ci.org/VitorLuizC/valite.svg?branch=master)](https://travis-ci.org/VitorLuizC/valite)

Concurrently execute your validators in a simple, practical and light validator engine.

## Motivation

I spent some time looking for a validation module that was simple, practical and light. All I found were modules that promise simplicity, but deliver complex APIs; they promise lightness, but deliver very heavy dependencies; promise practicality, but deliver ready-made functions that do not meet our needs, and it is necessary to download new modules and configure messages to change language and validation behavior.

So I wrote `valite`, unlike all of them, it's just the _core_ needed to build your validations. It is asynchronous by default, as everything should be in JavaScript, and has an extremely simple and concise API.

## Install

`valite` is published under NPM registry, so you can install from any package manager.

```sh
npm install valite --save

# Use this command for Yarn.
yarn add valite
```

## API

The API is composed by a validation function, `validate`, a validation object function `validateObject` and `isValid` which is a simple error checker.

#### `Validator`

Validators are functions that receives a value and returns a message or `true`.

```js
const isName = (name) => Boolean(name.trim()) || 'Name shouldn\'t be empty.';
```

For TypeScript, `valite` exports `Validator` type to improve your code safety.

```ts
import { Validator } from 'valite';

const isName: Validator = (name: string) => Boolean(name.trim()) || 'Name shouldn\'t be empty.';
```

#### `validate`

Executes validators and returns first obtained message or `null`.

```js
const mail = 'hi@capiwara.com.br';

validate(mail, [
  (mail) => Boolean(mail.trim()) || 'Mail is required.',
  (mail) => /^.+@.+\..+$/.test(mail) || 'Mail is invalid',

  // You can use async validators.
  (mail) => (
    services.isMailRegistered(mail)
      .then((isRegistered) => isRegistered || 'Mail is already registered.')
      .catch(() => 'Can\'t even verify if mail is already registered.')
  )
]);
//=> Promise { 'E-Mail is already registered.' };
```

#### `validateObject`

**Concurrently** validates an `object` using validators from a schema and returns them in same structure.

> Structure supports _dot notation_ for deep properties.

```js
const entries = {
  answer: document.querySelector('.answer').checked,
  user: {
    mail: document.querySelector('.mail').value,
    password: document.querySelector('.password').value,
  }
};

validateObject(entries, {
  'answer': [
    (answer) => Boolean(answer) || 'Terms should be accepted.',
  ],
  'user.mail': [
    (mail) => Boolean(value.trim()) || 'E-Mail is required.',
  ],
  'user.password': [
    (password) => Boolean(password.trim()) || 'Password is required.',
  ]
});
//=> Promise {{
//     'answer': null,
//     'user.mail': 'E-Mail is required',
//     'user.password': null
//   }}
```

#### `isValid`

Is a easy way to check if `validate` / `validateObject` payload has no errors.

```js
const payload = await validateObject(/* ... */);

isValid(payload);
//=> true
```

## License

Released under [MIT License](./LICENSE.md).

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