allow-react
allow-react
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.
Another goal is to eliminate the need for unit tests that only test a function's / method's operation when it receives the "wrong" kind of data. By validating that data at runtime, we are ensuring that the internal logic of the function / method is only invoked if it has been supplied with data of the expected type.
NOTE: This library is an extension of @toolz/allow, designed to handle specific cases where React elements need to be checked. For this reason, the react library is a core dependency of this package. But it does not exist in @toolz/allow. If you are not working in a React project, or if you don't feel the need to specifically check for React elements, it's recommended to use the core @toolz/allow package.
Usage
After installation, import the package:
import { allow } from '@toolz/allow-react';
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 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 = someValue => {
allow.aBoolean(someValue);
}
.aFunction()
const doSomething = theCallback => {
allow.aFunction(theCallback);
}
.anArray()
const doSomething = theArray => {
allow.anArray(theArray);
}
const doSomething = theArray => {
allow.anArray(theArray, 1);
}
const doSomething = theArray => {
allow.anArray(theArray, 2, 50);
}
.anArrayOfArrays()
const doSomething = theArrays => {
allow.anArrayOfArrays(theArrays);
}
const doSomething = theArrays => {
allow.anArrayOfArrays(theArrays, 1);
}
const doSomething = theArrays => {
allow.anArrayOfArrays(theArrays, 2, 50);
}
.anArrayOfInstances()
const person = {
firstName: '',
lastName: '',
middleInitial: '',
}
const doSomething = thePeople => {
allow.anArrayOfInstances(thePeople, person);
}
const person = {
firstName: '',
lastName: '',
middleInitial: '',
}
const doSomething = thePeople => {
allow.anArrayOfInstances(thePeople, person, 1);
}
const person = {
firstName: '',
lastName: '',
middleInitial: '',
}
const doSomething = thePeople => {
allow.anArrayOfInstances(thePeople, person, 2, 50);
}
.anArrayOfIntegers()
const doSomething = theNumbers => {
allow.anArrayOfIntegers(theNumbers);
}
const doSomething = theNumbers => {
allow.anArrayOfIntegers(theNumbers, 1);
}
const doSomething = theNumbers => {
allow.anArrayOfIntegers(theNumbers, 2, 50);
}
.anArrayOfNumbers()
const doSomething = theNumbers => {
allow.anArrayOfNumbers(theNumbers);
}
const doSomething = theNumbers => {
allow.anArrayOfNumbers(theNumbers, 1);
}
const doSomething = theNumbers => {
allow.anArrayOfNumbers(theNumbers, 2, 50);
}
.anArrayOfObjects()
const doSomething = theObjects => {
allow.anArrayOfObjects(theObjects);
}
const doSomething = theObjects => {
allow.anArrayOfObjects(theObjects, 1);
}
const doSomething = theObjects => {
allow.anArrayOfObjects(theObjects, 2, 50);
}
.anArrayOfReactElements()
const doSomething = theElements => {
allow.anArrayOfReactElements(theElements);
}
.anArrayOfStrings()
const doSomething = theStrings => {
allow.anArrayOfStrings(theStrings);
}
const doSomething = theStrings => {
allow.anArrayOfStrings(theStrings, 1);
}
const doSomething = theStrings => {
allow.anArrayOfStrings(theStrings, 2, 50);
}
.anInstanceOf()
const person = {
firstName: '',
lastName: '',
middleInitial: '',
}
const doSomething = thePerson => {
allow.anInstanceOf(thePerson, person);
}
const person = {
firstName: '',
lastName: '',
middleInitial: '',
address: {},
children: [],
}
const doSomething = thePerson => {
allow.anInstanceOf(thePerson, person);
}
.anInteger()
const doSomething = theNumber => {
allow.anInteger(theNumber);
}
const doSomething = theNumber => {
allow.anInteger(theNumber, 0);
}
const doSomething = theNumber => {
allow.anInteger(theNumber, 2, 50);
}
.anObject()
const doSomething = theObject => {
allow.anObject(theObject);
}
const doSomething = theObject => {
allow.anObject(theObject, 1);
}
const doSomething = theObject => {
allow.anObject(theObject, 2, 50);
}
.aReactElement()
const doSomething = theElement => {
allow.aReactElement(theElement);
}
.aString()
const doSomething = theString => {
allow.aString(theString);
}
const doSomething = theString => {
allow.aString(theString, 1);
}
const doSomething = theString => {
allow.aString(theString, 2, 50);
}
.getAllowNull()
console.log(allow.getAllowNull());
.getFailureBehavior()
console.log(allow.getFailureBehavior());
.getOnFailure()
allow.getOnFailure();
.oneOf()
This can be called two different ways - by passing in a reference array or a reference object.
const days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat'];
const doSomething = theDay => {
allow.oneOf(theDay, days);
}
const days = {
0: 'Sun',
1: 'Mon',
2: 'Tue',
3: 'Wed',
4: 'Thur',
5: 'Fri',
6: 'Sat',
}
const doSomething = theDay => {
allow.oneOf(theDay, days);
}
The first value passed into oneOf() must be a primitive. It cannot be an object, an array, a function, or a React element.
.setAllowNull()
const thisIsNull = null;
allow.anArray(thisIsNull);
allow.setAllowNull(true);
allow.anArray(thisIsNull);
.setFailureBehavior()
allow.setFailureBehavior('warn');
.setOnFailure()
const myCustomErrorHandler = (value, message) => {
}
allow.setFailureBehavior('ignore');
allow.setOnFailure(myCustomErrorHandler);
Chaining
A successful call to any of the allow validation methods always returns allow. This enables the chaining of multiple checks on a single line of code. That would look something like this:
const doSomething = (patient, isAlive, age) => {
allow.anObject(patient, 1).aBoolean(isAlive).aNumber(age, 0, 130);
}