New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

define-options

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

define-options

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

  • 0.1.3
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
11K
increased by12.58%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 13 May 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc