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

option-validator

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

option-validator

A simple option validator

  • 2.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.3K
increased by54.31%
Maintainers
1
Weekly downloads
 
Created
Source

option-validator

Build Status version license size npm Downloads dependencies Status

A simple option validator

Install

$ npm install option-validator
import optionValidator from 'option-validator';

OR umd builds are also available

<script src="path/to/option-validator.js"></script>

Will expose the global variable to window.optionValidator.

Usage

  • Only one api is to receive a option object and a scheme.
  • If the verification is passed, the original option object will be returned.
  • If the verification fails, an exception will be thrown.
  • Support all js type detection JS Types
  • Support for custom validator functions
const option = {
  a: 1,
  b: '2',
  c: {
    d: () => null,
  },
  g: {
    h: new Error('error'),
  },
  i: [1, '2', () => 3],
  j: [1, 2, 3, 4, 5, 6],
  k: '123456',
};

const scheme = {
  // Shallow verification
  a: 'number',
  b: 'string',

  // Deep verification
  c: {
    d: 'function',
  },

  // Validator functions
  g: {
    h: (value, type, path) => {
      // value --> new Error('error')
      // type --> 'error'
      // path --> ['option', 'g', 'h']

      // Returns string mean validation failed, and the string will thrown
      return 'I will throw an exception';

      // Returns error also mean validation failed, and the error will thrown
      return new Error('I will throw an exception');

      // Returns true mean verification passed
      return type === 'error';
    },
  },

  // Verify array elements
  i: ['number', 'string', 'function'],

  // If there is no corresponding validator, the first one is taken by default.
  j: ['number'],

  // Verify one of them
  k: 'number|string',
};

optionValidator(option, scheme);

License

MIT © Harvey Zack

Keywords

FAQs

Package last updated on 04 Sep 2020

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