# korrekt

> Async validation

Latest version **2.2.4** (published 2020-03-14) · MIT license · 0 weekly downloads

## Install

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

## 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 | 2.2.4 |
| Published | 2020-03-14 |
| First published | 2015-12-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 8.9.1 |
| Dependencies | 0 |
| Unpacked size | 34.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Constantin Titarenko |
| Maintainers | titarenko |
| Keywords | validator, validation, async, promise |

## Links

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

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 2.2.4 (latest) — 2020-03-14
- 2.2.3 — 2019-10-27
- 2.2.2 — 2019-10-23
- 2.2.1 — 2018-12-03
- 2.2.0 — 2018-01-10
- 2.1.2 — 2018-01-01
- 2.1.1 — 2018-01-01
- 2.1.0 — 2017-12-20
- 2.0.1 — 2017-12-20
- 2.0.0 — 2017-11-24
- 1.2.3 — 2016-11-30
- 1.2.2 — 2016-11-24
- 1.2.1 — 2016-11-24
- 1.2.0 — 2016-11-21
- 1.1.3 — 2016-11-18
- … 8 more at https://npm.io/package/korrekt/versions

## README

# korrekt

Asynchronous validation library.

[![Build Status](https://travis-ci.org/titarenko/korrekt.svg?branch=master)](https://travis-ci.org/titarenko/korrekt)
[![Coverage Status](https://coveralls.io/repos/github/titarenko/korrekt/badge.svg?branch=master)](https://coveralls.io/github/titarenko/korrekt?branch=master)

## TOC

* [Installation](#installation)
* [Usage](#usage)
	* [Out of the box](#out-of-the-box)
	* [Custom rules](#custom-rules)
* [Reference](#reference)
	* [List of methods](#list-of-methods)
	* [List of rules](#list-of-rules)
* [License](#license)

## Installation

```bash
npm i korrekt --save
```

## Usage

### Out of the box

```js
const v = require('korrekt')

const validator = v.create({
	name: v.length({ min: 3 }),
	email: v.email(),
	skype: v.all(
		v.length({ min: 3 }),
		v.match(/\w+/),
	),
	phone: v.when(it => !it.skype, v.all(
		v.match(/\d+/),
		v.length({ min: 9, max: 9 }),
	))
})

validator({ name: 'me', email: 'me;myself@world.com', skype: 'abba' })
	.then(validatedObject => console.log(validatedObject))
	.catch(v.ValidationError, error => console.error(error.result))

// { name: { message: 'must be longer', meta: { min: 3 } }, email: { message: 'must be an email' } }
```

### Custom rules

```js
const v = require('korrekt')

v.register('same', function (field) {
	return function (value, _, instance) {
		if (instance[field] != value) {
			return `must be same as ${field}`
		}
	}
})

const validator = v.create({
	name: v.length({ min: 3, max: 10 }),
	password: v.length({ min: 3, max: 40 }),
	password_confirmation: v.same('password'),
})

validator({ name: 'me123456789', password: '1', password_confirmation: '2' })
	.then(validatedObject => console.log(validatedObject))
	.catch(v.ValidationError, error => console.error(error.fields))

// { name: { message: 'must be shorter', meta: { max: 10 } }, password: { message: 'must be longer', meta: { min: 3 } }, password_confirmation: { message: 'must be same as  password' } }
```

## Reference

### List of methods

#### create(rule)

Creates validator function.

#### register(name, rule, [overwrite])

Registers custom rule. Rule parameter here is actually the rule builder, accepting options and custom message as arguments. By default `register` throws exception if rule with same name already exists, but you can specify `true` as 3rd argument to explicitly overwrite existing rule.

### List of rules

Rule | Description
--- | ---
required(optionalNestedRule) | Requires value to be present (not undefined or null). Executes `optionalNestedRule`, if specified.
length({ min, max, exactly }) | Verifies value has length and it is between specified boundaries (if any).
integer({ min, max }) | Verifies value is an integer and it is between specified boundaries (if any).
number({ min, max }) | Verifies value is a number (integer or real) and it is between specified boundaries (if any).
string({ min, max, exactly }) | Verifies value is a string and it's length is between specified boundaries (if any).
match(regex) | Verifies value matches regex.
enum(option1, option2, ...) | Verifies value is equal to one of specified options.
email() | Verifies value is an email (has @ inside).
when(predicate: instance => boolean, rule) | Verifies value is valid according to rule, but verification is done only if predicate returns true.
all(rule1, rule2, ...) | Verifies value is valid according to each rule from rules array.
any(rule1, rile2, ...) | Verifies value is valid according to at least one rule from rules array.
array(rule, { min, max, exactly }) | Verifies value is an array and each item of it is valid according to rule. Also checks array length if at least one boundary is specified.
object({ name: rule }) | Verifies value is an object and checks whether its fields are valid according to rules.
function({ min, max, exactly }) | Verifies value is a function and checks whether its arity falls between specified boundaries (if any).

## License

MIT

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