# extensible-validator

> Typescript-friendly and extensible object and value validation

Latest version **2.2.7** (published 2019-08-26) · ISC license · 0 weekly downloads

## Install

```sh
npm install extensible-validator
pnpm add extensible-validator
yarn add extensible-validator
bun add extensible-validator
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.7 |
| Published | 2019-08-26 |
| First published | 2017-07-12 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 37.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Stewart Everett |
| Maintainers | stewartml |
| Keywords | validator, validation, schema, typescript |

## Links

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

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.17.14
- [moment](https://npm.io/package/moment.md) ^2.24.0

## 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

- 2.2.7 (latest) — 2019-08-26
- 2.2.6 — 2019-08-12
- 2.2.5 — 2019-07-16
- 2.2.3 — 2019-06-17
- 2.2.2 — 2019-06-17
- 2.2.1 — 2019-06-17
- 2.2.0 — 2019-06-17
- 2.1.0 — 2019-06-16
- 2.0.2 — 2019-06-16
- 2.0.1 — 2019-06-16
- 2.0.0 — 2019-06-16
- 1.1.0 — 2017-07-30
- 1.0.1 — 2017-07-12

## README

# extensible-validator

Typescript-friendly and extensible object and value validation.

Also, unlike like other libraries where `validate` throws an error, I just return an array of errors, which could be empty. Throwing an error in the case of bad input from a function which exists to consider the case of bad input is ugly as hell.

I also separate validating and "casting" the input to a desired output format, because they're two separate operations.

## Usage

Install:

    $ npm install --save extensible-validator

Import, then define and use a schema:

```js
import { object, string, number } from 'extensible-validator';

let validator = object.keys({
  name: string.required(),
  age: number.integer(),
});

let person = { name: 'Bob', age: 32 };
console.log(validator.validate(person));
// []

let notAPerson = { age: "what's my age again?" };
console.log(validator.validate(notAPerson));
// [{message: 'required', path: 'name'}, {message: 'must be a number', path: 'age'}]
```

If you want to extend the validator functionality, you can either add tests to existing types:

```js
const objectId = string.addTest(value => ObjectId.isValid(value));

const person = object.keys({
  _id: objectId.required(),
  name: string.required(),
});
```

Or you can define a new schema class:

```js
class ObjectIdSchema extends Schema<ObjectId> {
  constructor() {
    super({ type: 'must be an ObjectId' }, value => ObjectId.isValid(value));
  }

  cast(value: any) {
    return value instanceof ObjectId ? value : new ObjectId(value);
  }
}
```

The latter gives you more control, and the ability to override the type test and the `cast` method.

> TODO

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