Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

assert-options

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

assert-options

Generic options parameter handling.

  • 0.8.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
322K
decreased by-1.67%
Maintainers
1
Weekly downloads
 
Created

What is assert-options?

The assert-options npm package is a utility for validating and asserting options in JavaScript objects. It helps ensure that the options passed to a function or module are valid and meet the expected criteria.

What are assert-options's main functionalities?

Basic Option Validation

This feature allows you to validate and merge user-provided options with default options. If the user does not provide an option, the default value is used.

const assertOptions = require('assert-options');

function myFunction(options) {
  const defaultOptions = { a: 1, b: 2 };
  options = assertOptions(options, defaultOptions);
  console.log(options);
}

myFunction({ a: 10 }); // { a: 10, b: 2 }
myFunction(); // { a: 1, b: 2 }

Strict Option Validation

This feature enforces strict validation, ensuring that only the specified options are allowed. If an unknown option is provided, an error is thrown.

const assertOptions = require('assert-options');

function myFunction(options) {
  const defaultOptions = { a: 1, b: 2 };
  options = assertOptions(options, defaultOptions, { strict: true });
  console.log(options);
}

myFunction({ a: 10, c: 3 }); // Throws an error because 'c' is not a valid option

Nested Option Validation

This feature supports validation of nested options, allowing you to merge and validate deeply nested objects.

const assertOptions = require('assert-options');

function myFunction(options) {
  const defaultOptions = { a: 1, b: { c: 2, d: 3 } };
  options = assertOptions(options, defaultOptions);
  console.log(options);
}

myFunction({ b: { c: 10 } }); // { a: 1, b: { c: 10, d: 3 } }
myFunction(); // { a: 1, b: { c: 2, d: 3 } }

Other packages similar to assert-options

Keywords

FAQs

Package last updated on 18 Mar 2023

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