# mcheck

[![npm version][npm-version-img]][npm-version-url]
[![build status][build-img]][build-url]
[![coverage status][coverage-img]][coverage-url]
[![npm dependencies][npm-dependencies-img]][npm-dependencies-url]

Minimalistic yet powerful and highly customizable validation.

## Installation
```
npm install mcheck
```

## Usage

### Basic
```js
const Check = require('mcheck')

const check = new Check()
check.val(6, 'rating').optional().int({ min: 1, max: 5 })

const body = { email: 'no mail' }
check.obj(email, 'email').required().string({ max: 50 }).email()

check.ok // false
check.errors[0] // { name: 'rating', code: 'int_rng', min: 1, max: 5 }
check.errors[1] // { name: 'email', code: 'email' }
```

### Custom validators
Use ``Check.add()`` to add new validators

```js
const Check = require('mcheck')

// register new validator
const cool = (ctx) => {
  ctx.ensure(val => val === 'cool', 'cool_err_code')
}

Check.add({ cool })

// use new validator
const check = new Check()
check.val('hot', 'my field').string().cool()

check.ok // false
check.errors[0] // { name: 'my field', code: 'cool_err_code' }
```

### Functions

**required()** - check if value is not ``null`` or ``undefined``
```js
check.val(null, 'f').required() // err: { name: 'f', code: 'req' }
```

**optional()** - if value is ``null`` or ``undefined`` result is valid even if other checks not
```js
check.val(null, 'f').optional().int() // ok: true
```

**bool()** - check if value is boolean
```js
check.val(5, 'f').bool() // err: { name: 'f', code: 'boolean' }
```

**int({ min, max })** - check if value is integer
* ``min`` - minimal value (included)
* ``max`` - maximal value (included)
```js
check.val('str', 'f').int() // err: { name: 'f', code: 'int' }
check.val(2, 'f').int({ min: 3 }) // err: { name: 'f', code: 'int_min', min: 3 }
check.val(2, 'f').int({ max: 1 }) // err: { name: 'f', code: 'int_max', max: 1 }
check.val(2, 'f').int({ min: 3, max: 5 }) // err: { name: 'f', code: 'int_rng', min: 3, max: 5 }
```

**number({ min, max })** - check if value is number
* ``min`` - minimal value (included)
* ``max`` - maximal value (included)
```js
check.val('str', 'f').number() // err: { name: 'f', code: 'num' }
check.val(2, 'f').number({ min: 3 }) // err: { name: 'f', code: 'num_min', min: 3 }
check.val(2, 'f').number({ max: 1 }) // err: { name: 'f', code: 'num_max', max: 1 }
check.val(2, 'f').number({ min: 3, max: 5 }) // err: { name: 'f', code: 'num_rng', min: 3, max: 5 }
```

**string({ lng, min, max })** - check if value is string
* ``lng`` - string length
* ``min`` - minimal string length (included)
* ``max`` - maximal string length (included)
* ``empty`` - allows string to be empty (default true)
```js
check.val(1, 'f').sting() // err: { name: 'f', code: 'str' }
check.val('str', 'f').string({ lng: 2 }) // err: { name: 'f', code: 'str_lng', lng: 2 }
check.val('str', 'f').string({ min: 5 }) // err: { name: 'f', code: 'str_min', min: 5 }
check.val('str', 'f').string({ max: 1 }) // err: { name: 'f', code: 'str_max', max: 1 }
check.val('str', 'f').string({ min: 5, max: 10 }) // err: { name: 'f', code: 'str_rng', min: 5, max: 10 }
check.val(' ', 'f').string() // err: { name: f, code: str_empty }
check.val(' ', 'f').string({ empty: true }) // ok: true
```

**email()** - check if value is email
```js
check.val('no mail', 'f').email() // err: { name: 'f', code: 'email' }
```

**object()** - check if value is object
```js
check.val(1, 'f').object() // err: { name: 'f', code: 'obj' }
```

**date({ min, max })** - check if value is date
* ``min`` - minimal date
* ``max`` - maximal date
```js
const y05 = new Date(2005, 1, 1)
const y10 = new Date(2010, 1, 1)
const y15 = new Date(2015, 1, 1)

check.val(1, 'f').date() // err: { name: 'f', code: 'date' }
check.val(y05, 'f').date({ min: y10 }) // err: { name: 'f', code: 'date_min', min: 2010-01-31.. }
check.val(y10, 'f').date({ max: y05 }) // err: { name: 'f', code: 'date_max', max: 2005-01-31.. }
check.val(y15, 'f').date({ min: y05, max: y10 }) // err: { name: 'f', code: 'date_rng', min: 2005-01-31.., max: 2010-01-31.. }
```

**func()** - check if value is func
```js
check.val(1, 'f').func() // err: { name: 'f', code: 'func' }
```

**array({ lng, min, max })** - check if value is array
* ``lng`` - array length
* ``min`` - minimal array length
* ``max`` - maximal array length
```js
check.val(1, 'f').array() // err: { name: 'f', code: 'array' }
check.val([], 'f').array({ lng: 1 }) // err: { name: 'f', code: 'array_lng', lng: 1 }
check.val([], 'f').array({ min: 1 }) // err: { name: 'f', code: 'array_min', min: 1 }
check.val([1,2], 'f').array({ max: 1 }) // err: { name: 'f', code: 'array_max', max: 1 }
check.val([], 'f').array({ min: 1, max: 2 }) // err: { name: 'f', code: 'array_rng', min: 1, max: 2 }
```

## Licence
MIT

[npm-version-url]: https://www.npmjs.com/package/mcheck
[npm-version-img]: https://img.shields.io/npm/v/mcheck.svg?style=flat-square
[npm-dependencies-url]: https://david-dm.org/ct0r/mcheck
[npm-dependencies-img]: https://img.shields.io/david/ct0r/mcheck.svg?style=flat-square
[build-url]: https://circleci.com/gh/ct0r/mcheck
[build-img]: https://img.shields.io/circleci/project/github/ct0r/mcheck.svg?style=flat-square
[coverage-url]: https://codecov.io/gh/ct0r/mcheck
[coverage-img]: https://img.shields.io/codecov/c/github/ct0r/mcheck.svg?style=flat-square
