# @mbokinala/data-validator

> A data validation library designed to validate JSON data for REST APIs and provide useful validation error output

Latest version **0.2.0** (published 2021-08-06) · ISC license · 0 weekly downloads

## Install

```sh
npm install @mbokinala/data-validator
pnpm add @mbokinala/data-validator
yarn add @mbokinala/data-validator
bun add @mbokinala/data-validator
```

## Health

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

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2021-08-06 |
| First published | 2021-08-06 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 22.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Manav Bokinala |
| Maintainers | mbokinala |
| Keywords | validation, rest, api |

## Links

- npm: https://www.npmjs.com/package/@mbokinala/data-validator
- Repository: https://github.com/mbokinala/data-validator
- Homepage: https://github.com/mbokinala/data-validator#readme
- Issues: https://github.com/mbokinala/data-validator/issues
- npm.io page: https://npm.io/package/@mbokinala/data-validator

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 0.2.0 (latest) — 2021-08-06
- 0.1.3 — 2021-08-06
- 0.1.2 — 2021-08-06
- 0.1.1 — 2021-08-06
- 0.1.0 — 2021-08-06

## README

# Data Validation

A (work in progress) data validation library designed to validate JSON data for REST APIs and provide useful validation error output

## Installation
```shell
npm install @mbokinala/data-validator
```
or
```shell
yarn add @mbokinala/data-validator
```

## Example
```js
const {validate, isDefined, isNotEmpty, isType, isGreaterThan, validateArray, includes} = require('@mbokinala/data-validator');

let data = {
    age: 18,
    profession: false,
    favoriteLanguages: [
        'java',
        'go',
        12,
        'python'
    ]
}

let [valid, errors] = validate(data, {
    name: [isDefined, isType('string'), isNotEmpty],
    age: [isDefined, isType('number'), isGreaterThan(21)],
    profession: [isDefined, isType('string')],
    favoriteLanguages: [isDefined, validateArray([isDefined, isType('string')]), includes('typescript')]
})

console.log('Validation result is', valid);

if (!valid) {
    console.log(JSON.stringify(errors, null, 4));
} else {
    console.log('valid');
}
```
Output:
```
Validation result is false
{
    "name": [
        "is null or undefined",
        "is not of type `string`",
        "is empty"
    ],
    "age": [
        "is not greater than 21"
    ],
    "profession": [
        "is not of type `string`"
    ],
    "favoriteLanguages": [
        {
            "2": [
                "is not of type `string`"
            ]
        },
        "does not include `typescript`"
    ]
}
```

## Usage
`validate(data, validators) => [valid: boolean, errors? ValidationErrors]`
The library is designed so that you can return `errors` as a JSON object in something like a REST API.

`validators` should match the structure of the data you want to validate. For each key, the value should be an array of validator functions to be run on the field.
There are some built-in validator functions (see below), but you can create custom validators using the format:

```js
// valid: true if input passes the validation test, false if otherwise
// message: an error message to be displayed if validation fails
(input: any) => [valid: boolean, message?: string];
```

### Built-in validators:
| Function                                         	| Description                                                                                                                                         	|
|--------------------------------------------------	|-----------------------------------------------------------------------------------------------------------------------------------------------------	|
| `isDefined`                                      	| Passes if input is not `null` or `undefined`                                                                                                        	|
| `isType(type: string)`                           	| Passes if input matches provided type                                                                                                               	|
| `isArray`                                        	| Passes if input is an array                                                                                                                         	|
| `isNotEmpty`                                     	| Passes if input string or array is not empty                                                                                                        	|
| `equals(val: any)`                               	| Passes if input matches provided value                                                                                                              	|
| `includes(element: any)`                         	| Passes if input array contains provided element                                                                                                     	|
| `isGreaterThan(val: number)`                     	| Passes if input is greater than provided value                                                                                                      	|
| `isGreaterThanOrEqualTo(val: number)`            	| Passes if input is greater than or equal to provided value                                                                                          	|
| `isLessThan(val: number)`                        	| Passes if input is less than provided value                                                                                                         	|
| `isLessThanOrEqualTo(val: number)`               	| Passes if input is less than or equal to provided value                                                                                             	|
| `validateArray(validators: ValidatorFunction[])` 	| Passes if all elements in input array pass all provided validators                                                                                  	|
| `ifDefined(validators: ValidatorFunction[])`     	| Passes if input is defined or if input passes all provided validators. Useful for when a validator is only meant to be run when a value is defined. 	|

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