Socket
Socket
Sign inDemoInstall

@toolz/allow

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@toolz/allow

Provides validation of data types


Version published
Weekly downloads
494
decreased by-10.51%
Maintainers
1
Weekly downloads
 
Created
Source

allow

allow is a library that checks data types and allows the script to continue if they pass the check. If the check fails, the script can throw an Error, or emit a warning, or invoke a custom callback. The package was written to ensure that only the "right" kind of data is allowed into the body of a function / method / component / etc. The intention is to provide effective runtime validation of data before it reaches application logic.

Usage

After installation, import the package as such:

import { allow } from '@toolz/allow';

Once imported, the assumed usage is directly after the entry to any function / method / component / etc. The idea is to check the integrity of provided inputs before further computation proceeds. This would typically look like this:

const addSalesTax = originalPrice => {
   allow.aNumber(originalPrice, 0);
   /*
      ...proceed with the rest of the function
    */
}

In the above example, the assumption is that originalPriceshould always be a number. If any other data type is provided for originalPrice, the allowcheck will fail. This means that a value of '32.99'will fail (because it's a string). nullwill fail. Boolean values will fail. Anything that is not a number will fail. In this example, the second argument (which is optional), indicates the minimum acceptable value of the number. In this case, we don't want negative values for originalPrice, so nothing below 0will pass the check.

Methods

aBoolean

const doSomething = reallyDoIt => {
   allow.aBoolean(reallyDoIt);
   /*
      This is NOT "truthy".  It fails if anything other than a true Boolean is 
      provided.  This means that it fails on 'TRUE'/'FALSE' (because they're 
      strings), on 1/0 (because they're numbers), or any other value that is 
      not a pure TRUE/FALSE
    */
}

aFunction

const doSomething = callback => {
   allow.aFunction(callback);
   /*
      This will fail unless a function is provided as the value for callback
    */
}

anArray

const doSomething = theValues => {
   allow.anArray(theValues);
   /*
       This will fail unless an array is provided as the value for theValues.  
       In this example, theValues can be an empty array - but it must still be 
       an array.
     */
}
const doSomething = theValues => {
   allow.anArray(theValues, 0);
   /*
       The second argument of anArray() is the minimum length of the array.  So, 
       by setting this value to 0, it ensures that theValues is a non-empty 
       array.
     */
}
const doSomething = theValues => {
   allow.anArray(theValues, 2, 50);
   /*
       This ensures that theValues is an array, that is has no fewer than 2 
       elements, and no more than 50 elements.
     */
}

anArrayOfArrays

const doSomething = nestedArray => {
   allow.anArrayOfArrays(nestedArray);
   /*
       This will fail unless an array is provided as the value for nestedArray.  
       It will also fail if any of elements inside nestedArray are not also 
       arrays.  If you need something that will ensure that the array, INSIDE the 
       array, also contains more arrays, you need to get a life and publish your 
       own damn NPM package.
     */
}
const doSomething = nestedArray => {
   allow.anArrayOfArrays(nestedArray, 0);
   /*
       The second argument of anArrayOfArrays() is the minimum length of the array.  
       So, by setting this value to 0, it ensures that nestedArray is a non-empty 
       array-of-arrays.
     */
}
const doSomething = nestedArray => {
   allow.anArray(nestedArray, 2, 50);
   /*
       This ensures that nestedArray is an array, that all of its elements are 
       arrays, that is has no fewer than 2 elements, and no more than 50 elements .
     */
}

Keywords

FAQs

Package last updated on 19 Feb 2021

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