# validate-or-throw

> Small fastest-validator wrapper that either validates an object against a given schema or throws an error.

Latest version **1.0.2** (published 2022-08-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install validate-or-throw
pnpm add validate-or-throw
yarn add validate-or-throw
bun add validate-or-throw
```

## 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 | 1.0.2 |
| Published | 2022-08-24 |
| First published | 2022-08-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 3.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Anthony Mugendi |
| Maintainers | nguru |
| Keywords | fastest-validator, validate, error, throw, schema |

## Links

- npm: https://www.npmjs.com/package/validate-or-throw
- Repository: https://github.com/mugendi/validate-or-throw
- Homepage: https://github.com/mugendi/validate-or-throw#readme
- Issues: https://github.com/mugendi/validate-or-throw/issues
- npm.io page: https://npm.io/package/validate-or-throw

## Dependencies (1)

- [fastest-validator](https://npm.io/package/fastest-validator.md) ^1.13.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

- 1.0.2 (latest) — 2022-08-24
- 1.0.1 — 2022-08-24
- 1.0.0 — 2022-08-24

## README

# validate-or-throw

A tiny wrapper around fastest-validator to either validate an object against a given schema or throw an error.

![screenshot](https://repository-images.githubusercontent.com/528328110/a6a53839-d4fa-4c51-b174-6dda72a659c4)

## To use

```javascript
const ValidateOrThrow = require('validate-or-throw');

let obj = {
	str: 'this is a str',
	num: 2,
};

let schema = {
	str: 'string',
	num: 'number',
};

ValidateOrThrow(obj, schema);
```

## Why?

This wrapper was written to quickly validate options passed to methods or class constructors. More like [aproba](https://www.npmjs.com/package/aproba) is used to do, but with the power and flexibility that [fastest-validator](https://www.npmjs.com/package/fastest-validator) brings (like adding default values).

That is why it throws early to help avoid the scenario where you are using mis-formatted options and other object arguments.

# Example usage with a class constructor

```javascript
const validateOrThrow = require('validate-or-throw');

class Developer {
	constructor(options) {
		let schema = {
			// we expect one option, name to be a string
			name: 'string',
            // we also expect an age option, but where none is entered, we default to 99
			age: { type: 'number', optional: true, default: 99 },
		};

		// Now Validate or throw error so we do not continue with wrong options
		validateOrThrow(options, schema);

		this.options = options;

        // proceed with the rest of the code safely...
	}
}

let dev = new Developer({ name: 'Anthony Mugendi' });

console.log(dev);

// => Developer { options: { name: 'Anthony Mugendi', age: 99 } }
```

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