# define-options

> Define valid properties for an options litteral, and return a function that can validate the options

Latest version **0.1.3** (published 2014-05-13) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.3 |
| Published | 2014-05-13 |
| First published | 2014-03-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | FINN.no AS |
| Maintainers | gregersrygg |
| Keywords | browser, options, validate, util |

## Links

- npm: https://www.npmjs.com/package/define-options
- Repository: git@github.com:finn-no/define-options
- Homepage: https://github.com/finn-no/define-options
- Issues: https://github.com/finn-no/define-options/issues
- npm.io page: https://npm.io/package/define-options

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.1.3 (latest) — 2014-05-13
- 0.1.2 — 2014-03-31
- 0.1.1 — 2014-03-31
- 0.1.0 — 2014-03-31
- 0.0.1 — 2014-03-27

## README

define-options
==============

Micro lib to define valid properties for an options litteral, and return a function that can validate the options.

The main benefit is self-documenting code. Users of your library doesn't have to scan through the code to see what
properties from the options object that is used throughout the code. As a bonus you can easily throw an error early
if a consumer is using an option with the wrong type, or a required option is missing. They will get a hint from the
error message what they did wrong instead of searching for docs.

# API

    var defineOpts   = require('define-options');
    var validateOpts = defineOpts({
            optionalParam   : '*             - This parameter can be null, undefined or any value',
            optionalString  : '?|string      - This is an optional string',
            aString         : 'string        - Required string. Can be blank.',
            aNumber         : 'number        - Required number. Can be 0.',
            aNumberOrString : 'number|string - Required number or string',
            aStringArray    : 'array         - Required array of any types',
            aStringArray    : 'string[]      - Required array with strings',
            aNumberArray    : 'number[]      - Required array with numbers',
            anObject        : 'object        - Required object',
            aFunction       : 'function      - Required function',
            aBboolean       : 'boolean       - Required boolean'
        });

    function yourAPI(options) {
      validateOpts(options); // throws an error if options doesn't validate

      // do your stuff
    }

# Optional options object
If all options are optional, the null will not throw an error.

    var validateOpts = defineOpts({ foo: '?|string' });
    validateOpts();               //all OK
    validateOpts({foo: 'bar'});   // all OK
    validateOpts({foo: 123});     // Exception: foo has to be of type ?|string but was number

# Valid types

    'array', 'boolean', 'function', 'number', 'object', 'string'

# Arrays with type
Just add [] after the type

    'array[]', 'boolean[]', 'function[]', 'number[]', 'object[]', 'string[]'

# Releasing

    npm run build
    git add dist/
    git commit -m "Updated dist files"
    npm version [<newversion> | major | minor | patch]
    git push --follow-tags origin master
    npm publish

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