# option-validator

> A simple option validator

Latest version **2.0.6** (published 2020-09-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install option-validator
pnpm add option-validator
yarn add option-validator
bun add option-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 | 2.0.6 |
| Published | 2020-09-04 |
| First published | 2018-10-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 12.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Harvey Zack |
| Maintainers | zhw2590582 |
| Keywords | option, validator |

## Links

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

## Dependencies (1)

- [kind-of](https://npm.io/package/kind-of.md) ^6.0.3

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 2.0.6 (latest) — 2020-09-04
- 2.0.5 — 2019-10-13
- 2.0.4 — 2019-09-08
- 2.0.3 — 2019-09-05
- 2.0.2 — 2019-09-05
- 2.0.1 — 2019-09-05
- 2.0.0 — 2019-09-03
- 1.1.5 — 2019-09-02
- 1.1.4 — 2019-09-02
- 1.1.3 — 2019-08-15
- 1.1.2 — 2019-06-26
- 1.1.1 — 2018-11-14
- 1.1.0 — 2018-11-02
- 1.0.9 — 2018-11-02
- 1.0.8 — 2018-11-02
- … 8 more at https://npm.io/package/option-validator/versions

## README

# option-validator

[![Build Status](https://travis-ci.com/zhw2590582/option-validator.svg?branch=master)](https://travis-ci.com/zhw2590582/option-validator)
![version](https://badgen.net/npm/v/option-validator)
![license](https://badgen.net/npm/license/option-validator)
![size](https://badgen.net/bundlephobia/minzip/option-validator)
[![npm Downloads](https://img.shields.io/npm/dt/option-validator.svg)](https://www.npmjs.com/package/option-validator)
[![dependencies Status](https://david-dm.org/zhw2590582/option-validator/status.svg)](https://david-dm.org/zhw2590582/option-validator)

> A simple option validator

## Install

```
$ npm install option-validator
```

```js
import optionValidator from 'option-validator';
```

OR umd builds are also available

```html
<script src="path/to/option-validator.js"></script>
```

Will expose the global variable to `window.optionValidator`.

## Usage

- Only one api is to receive a option object and a scheme.
- If the verification is passed, the original option object will be returned.
- If the verification fails, an exception will be thrown.
- Support all js type detection [JS Types](./__test__/testData.js)
- Support for custom validator functions

```js
const option = {
  a: 1,
  b: '2',
  c: {
    d: () => null,
  },
  g: {
    h: new Error('error'),
  },
  i: [1, '2', () => 3],
  j: [1, 2, 3, 4, 5, 6],
  k: '123456',
};

const scheme = {
  // Shallow verification
  a: 'number',
  b: 'string',

  // Deep verification
  c: {
    d: 'function',
  },

  // Validator functions
  g: {
    h: (value, type, path) => {
      // value --> new Error('error')
      // type --> 'error'
      // path --> ['option', 'g', 'h']

      // Returns string mean validation failed, and the string will thrown
      return 'I will throw an exception';

      // Returns error also mean validation failed, and the error will thrown
      return new Error('I will throw an exception');

      // Returns true mean verification passed
      return type === 'error';
    },
  },

  // Verify array elements
  i: ['number', 'string', 'function'],

  // If there is no corresponding validator, the first one is taken by default.
  j: ['number'],

  // Verify one of them
  k: 'number|string',
};

optionValidator(option, scheme);
```

## License

MIT © [Harvey Zack](https://sleepy.im/)

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