# check-options

> Check an options object for required fields, invalid fields, and apply defaults.

Latest version **1.1.0** (published 2016-10-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install check-options
pnpm add check-options
yarn add check-options
bun add check-options
```

## 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 | 1.1.0 |
| Published | 2016-10-30 |
| First published | 2016-10-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Marek Zaluski |
| Maintainers | marekweb |

## Links

- npm: https://www.npmjs.com/package/check-options
- npm.io page: https://npm.io/package/check-options

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.16.4
- [pluralize](https://npm.io/package/pluralize.md) ^2.0.0

## Recent versions

- 1.1.0 (latest) — 2016-10-30
- 1.0.0 — 2016-10-30

## README

# check-options

Check for required fields in an options object, and optionally apply defaults.

### FAQ

- Is it strict? Yes, it will throw an exception if the options contain invalid fields (fields which are neither required nor present in the defaults).
- Does it modify the input options object? No, it returns a copy.

### Usage: checking required options

```js
function myFunction(options) {
  options = checkOptions(options, ['size', 'color']);

  // ...
}

// Will throw: Missing field (color)
myFunction({size: 10});

// Will throw: Missing field (color); Invalid field (colr)
myFunction({size: 10, colr: 'red'});
```

### Usage: applying default values

```js
function myFunction(options) {
  options = checkOptions(options, ['size'], {color: red});

  // ...
}

// Will throw: Invalid field (colr);
myFunction({colr: 'blue'});
```

### Usage: context name for better error messages

```js
function myFunction(options) {
  // Pass an optional context string as the last argument:
  options = checkOptions(options, ['size', 'color'], 'myFunction');

  // ...
}

// The context string will appear in the error message.
// Will throw: "myFunction: Missing field (size)""
myFunction({color: 'blue'});
```

### Taking advantage of destructuring syntax

```js
function myFunction(options) {
  const {width, height, color} = checkOptions(options, ['width', 'height', 'color']);


  // ...
}
```

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