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);
}
In the above example, the assumption is that originalPrice
should always be a number. If any other data type is provided for originalPrice
, the allow
check will fail. This means that a value of '32.99'
will fail (because it's a string). null
will 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 0
will pass the check.
Methods
aBoolean
const doSomething = reallyDoIt => {
allow.aBoolean(reallyDoIt);
}
aFunction
const doSomething = callback => {
allow.aFunction(callback);
}
anArray
const doSomething = theValues => {
allow.anArray(theValues);
}
const doSomething = theValues => {
allow.anArray(theValues, 0);
}
const doSomething = theValues => {
allow.anArray(theValues, 2, 50);
}