@toolz/allow
Advanced tools
Comparing version 0.0.19 to 1.0.0
{ | ||
"name": "@toolz/allow", | ||
"version": "0.0.19", | ||
"version": "1.0.0", | ||
"description": "Provides validation of data types", | ||
@@ -42,4 +42,11 @@ "keywords": [ | ||
"jest": "^26.6.3", | ||
"jest-matcher-one-of": "^1.0.2", | ||
"nodemon": "^2.0.4" | ||
} | ||
}, | ||
"browserslist": [ | ||
"last 1 chrome version", | ||
"last 1 firefox version", | ||
"last 1 safari version", | ||
"last 1 ie version" | ||
] | ||
} |
@@ -56,4 +56,5 @@ # allow | ||
But it must be a function of some kind. | ||
In JavaScript, a class will also show typeof 'function'. However, this | ||
check will fail if a class is passed into theCallback. | ||
Please note that, in JavaScript, a class is virtually indistinguishable | ||
from a function (since "class" is just syntactic sugar). For this reason, | ||
a class will also pass this check. | ||
*/ | ||
@@ -597,2 +598,4 @@ } | ||
The first value passed into `oneOf()` must be a primitive. It cannot be an object, an array, or a function. | ||
### .setAllowNull() | ||
@@ -599,0 +602,0 @@ |
@@ -17,4 +17,2 @@ const Allow = () => { | ||
return fail(value, 'is not a function'); | ||
if (!value.prototype.constructor.toString().includes('Function()')) | ||
return fail(value, 'is not a function'); | ||
return allow; | ||
@@ -185,4 +183,10 @@ }; | ||
const oneOf = (value, allowedValues) => { | ||
if (allowNull && value === null) | ||
return allow; | ||
if (isAnObject(value) || Array.isArray(value) || typeof value === 'function') { | ||
fail(value, 'cannot be an object, array, or function'); | ||
return allow; | ||
} | ||
if (typeof allowedValues !== 'object' || allowedValues === null) { | ||
fail(allowedValues, 'oneOf allowedValues must be an object or an array'); | ||
fail(allowedValues, 'allowedValues must be supplied in an object or an array'); | ||
return allow; | ||
@@ -189,0 +193,0 @@ } |
import { allow } from './allow'; | ||
import 'jest-matcher-one-of'; | ||
@@ -41,6 +42,2 @@ const briefPerson = { | ||
class AClass { | ||
foo = 'bar'; | ||
} | ||
const aFalse = false; | ||
@@ -72,6 +69,15 @@ const aFalseString = 'false'; | ||
let totalConsoleWarnings = 0; | ||
let totalOnFailureInvokations = 0; | ||
const customOnFailure = () => { | ||
totalOnFailureInvokations++; | ||
return false; | ||
}; | ||
beforeEach(() => { | ||
jest.spyOn(console, 'error'); | ||
console.error.mockImplementation(() => null); | ||
jest.spyOn(console, 'warn'); | ||
console.warn.mockImplementation(() => totalConsoleWarnings++); | ||
}); | ||
@@ -81,2 +87,3 @@ | ||
console.error.mockRestore(); | ||
console.warn.mockRestore(); | ||
}); | ||
@@ -98,3 +105,2 @@ | ||
test('aBoolean() should throw an error when checking anything other than a Boolean value', () => { | ||
expect(() => allow.aBoolean(AClass)).toThrow(); | ||
expect(() => allow.aBoolean(aFalseString)).toThrow(); | ||
@@ -138,3 +144,2 @@ expect(() => allow.aBoolean(aFunction)).toThrow(); | ||
test('aFunction() should throw an error when checking anything other than a function', () => { | ||
expect(() => allow.aFunction(AClass)).toThrow(); | ||
expect(() => allow.aFunction(aFalse)).toThrow(); | ||
@@ -217,3 +222,2 @@ expect(() => allow.aFunction(aFalseString)).toThrow(); | ||
test('anArray() should throw an error when checking anything other than an array', () => { | ||
expect(() => allow.anArray(AClass)).toThrow(); | ||
expect(() => allow.anArray(aFalse)).toThrow(); | ||
@@ -265,3 +269,2 @@ expect(() => allow.anArray(aFalseString)).toThrow(); | ||
test('anArrayOfArrays() should throw an error when checking anything other than an array-of-arrays', () => { | ||
expect(() => allow.anArrayOfArrays(AClass)).toThrow(); | ||
expect(() => allow.anArrayOfArrays(aFalse)).toThrow(); | ||
@@ -332,3 +335,2 @@ expect(() => allow.anArrayOfArrays(aFalseString)).toThrow(); | ||
test('anArrayOfInstances() should throw an error when checking anything other than an array-of-instances', () => { | ||
expect(() => allow.anArrayOfInstances(AClass, person)).toThrow(); | ||
expect(() => allow.anArrayOfInstances(aFalse, person)).toThrow(); | ||
@@ -386,3 +388,2 @@ expect(() => allow.anArrayOfInstances(aFalseString, person)).toThrow(); | ||
test('anArrayOfIntegers() should throw an error when checking anything other than an array-of-integers', () => { | ||
expect(() => allow.anArrayOfIntegers(AClass)).toThrow(); | ||
expect(() => allow.anArrayOfIntegers(aFalse)).toThrow(); | ||
@@ -441,3 +442,2 @@ expect(() => allow.anArrayOfIntegers(aFalseString)).toThrow(); | ||
test('anArrayOfNumbers() should throw an error when checking anything other than an array-of-numbers', () => { | ||
expect(() => allow.anArrayOfNumbers(AClass)).toThrow(); | ||
expect(() => allow.anArrayOfNumbers(aFalse)).toThrow(); | ||
@@ -495,3 +495,2 @@ expect(() => allow.anArrayOfNumbers(aFalseString)).toThrow(); | ||
test('anArrayOfObjects() should throw an error when checking anything other than an array-of-objects', () => { | ||
expect(() => allow.anArrayOfObjects(AClass)).toThrow(); | ||
expect(() => allow.anArrayOfObjects(aFalse)).toThrow(); | ||
@@ -548,3 +547,2 @@ expect(() => allow.anArrayOfObjects(aFalseString)).toThrow(); | ||
test('anArrayOfStrings() should throw an error when checking anything other than an array-of-strings', () => { | ||
expect(() => allow.anArrayOfStrings(AClass)).toThrow(); | ||
expect(() => allow.anArrayOfStrings(aFalse)).toThrow(); | ||
@@ -609,3 +607,2 @@ expect(() => allow.anArrayOfStrings(aFalseString)).toThrow(); | ||
test('anInstanceOf() should throw an error when checking anything other than an instance of the model object', () => { | ||
expect(() => allow.anInstanceOf(AClass, person)).toThrow(); | ||
expect(() => allow.anInstanceOf(aFalse, person)).toThrow(); | ||
@@ -675,3 +672,2 @@ expect(() => allow.anInstanceOf(aFalseString, person)).toThrow(); | ||
test('anInteger() should throw an error when checking anything other than an integer', () => { | ||
expect(() => allow.anInteger(AClass)).toThrow(); | ||
expect(() => allow.anInteger(aFalse)).toThrow(); | ||
@@ -735,3 +731,2 @@ expect(() => allow.anInteger(aFalseString)).toThrow(); | ||
test('anObject() should throw an error when checking anything other than an integer', () => { | ||
expect(() => allow.anObject(AClass)).toThrow(); | ||
expect(() => allow.anObject(aFalse)).toThrow(); | ||
@@ -807,3 +802,2 @@ expect(() => allow.anObject(aFalseString)).toThrow(); | ||
test('aNumber() should throw an error when checking anything other than a number', () => { | ||
expect(() => allow.aNumber(AClass)).toThrow(); | ||
expect(() => allow.aNumber(aFalse)).toThrow(); | ||
@@ -865,3 +859,2 @@ expect(() => allow.aNumber(aFalseString)).toThrow(); | ||
test('aString() should throw an error when checking anything other than a string', () => { | ||
expect(() => allow.aString(AClass)).toThrow(); | ||
expect(() => allow.aString(aFalse)).toThrow(); | ||
@@ -889,1 +882,169 @@ expect(() => allow.aString(aFunction)).toThrow(); | ||
}); | ||
// getAllowNull() | ||
test('getAllowNull() should return a Boolean', () => { | ||
expect(allow.getAllowNull()).toEqual(expect.any(Boolean)); | ||
}); | ||
// getFailureBehavior() | ||
test('getFailureBehavior() should return ignore, throw, or warn', () => { | ||
expect(allow.getFailureBehavior()).toBeOneOf(['ignore', 'throw', 'warn']); | ||
}); | ||
// getOnFailure() | ||
test('getOnFailure() should return a function', () => { | ||
expect(allow.getOnFailure()).toEqual(expect.any(Function)); | ||
}); | ||
// oneOf() | ||
test('oneOf() should pass when checking any value that exists in the allowedValues array', () => { | ||
expect(allow.oneOf(42, anArrayOfIntegers)).toEqual(expect.any(Object)); | ||
expect(allow.oneOf(3.14, anArrayOfNumbers)).toEqual(expect.any(Object)); | ||
expect(allow.oneOf('two', anArrayOfStrings)).toEqual(expect.any(Object)); | ||
}); | ||
test('oneOf() should pass when checking any value that exists in the allowedValues object', () => { | ||
expect(allow.oneOf('for', aPopulatedObject)).toEqual(expect.any(Object)); | ||
expect(allow.oneOf('jake', aPerson)).toEqual(expect.any(Object)); | ||
}); | ||
test('after setting allowNull to TRUE, oneOf() should pass when checking a NULL value', () => { | ||
allow.setAllowNull(true); | ||
expect(allow.oneOf(aNull, anArrayOfIntegers)).toEqual(expect.any(Object)); | ||
allow.setAllowNull(false); | ||
}); | ||
test('oneOf() should throw an error when checking a value that is an object, an array, or a function', () => { | ||
expect(() => allow.oneOf(anArrayOfNumbers, anArrayOfIntegers)).toThrow(); | ||
expect(() => allow.oneOf(aPopulatedObject, anArrayOfObjects)).toThrow(); | ||
expect(() => allow.oneOf(aFunction, anArrayOfIntegers)).toThrow(); | ||
}); | ||
test('oneOf() should throw an error when checking any value that does not exist in the allowedValues array', () => { | ||
expect(() => allow.oneOf(987, anArrayOfIntegers)).toThrow(); | ||
expect(() => allow.oneOf(101010, anArrayOfNumbers)).toThrow(); | ||
expect(() => allow.oneOf('fruity', anArrayOfStrings)).toThrow(); | ||
}); | ||
test('oneOf() should throw an error when checking any value that does not exist in the allowedValues object', () => { | ||
expect(() => allow.oneOf('foo', aPopulatedObject)).toThrow(); | ||
expect(() => allow.oneOf('reginald', aPerson)).toThrow(); | ||
}); | ||
// setAllowNull() | ||
test('setAllowNull() should change the behavior when null values are checked', () => { | ||
allow.setAllowNull(true); | ||
expect(allow.oneOf(aNull, anArrayOfIntegers)).toEqual(expect.any(Object)); | ||
allow.setAllowNull(false); | ||
expect(() => allow.oneOf(aNull, anArrayOfIntegers)).toThrow(); | ||
}); | ||
test('setAllowNull() should throw an error when supplied a non-Boolean value', () => { | ||
expect(() => allow.setAllowNull(aFalseString)).toThrow(); | ||
expect(() => allow.setAllowNull(aFunction)).toThrow(); | ||
expect(() => allow.setAllowNull(anArrayOfArrays)).toThrow(); | ||
expect(() => allow.setAllowNull(anArrayOfIntegers)).toThrow(); | ||
expect(() => allow.setAllowNull(anArrayOfMixedTypes)).toThrow(); | ||
expect(() => allow.setAllowNull(anArrayOfNumbers)).toThrow(); | ||
expect(() => allow.setAllowNull(anArrayOfObjects)).toThrow(); | ||
expect(() => allow.setAllowNull(anArrayOfPeople)).toThrow(); | ||
expect(() => allow.setAllowNull(anArrayOfStrings)).toThrow(); | ||
expect(() => allow.setAllowNull(aNegative1)).toThrow(); | ||
expect(() => allow.setAllowNull(aNegativePi)).toThrow(); | ||
expect(() => allow.setAllowNull(anEmptyArray)).toThrow(); | ||
expect(() => allow.setAllowNull(anEmptyObject)).toThrow(); | ||
expect(() => allow.setAllowNull(anEmptyString)).toThrow(); | ||
expect(() => allow.setAllowNull(aNull)).toThrow(); | ||
expect(() => allow.setAllowNull(aNumber0)).toThrow(); | ||
expect(() => allow.setAllowNull(aNumber1)).toThrow(); | ||
expect(() => allow.setAllowNull(aNumber1WithDecimals)).toThrow(); | ||
expect(() => allow.setAllowNull(anUndefined)).toThrow(); | ||
expect(() => allow.setAllowNull(aPi)).toThrow(); | ||
expect(() => allow.setAllowNull(aPopulatedObject)).toThrow(); | ||
expect(() => allow.setAllowNull(aTrueString)).toThrow(); | ||
}); | ||
// setFailureBehavior() | ||
test('setFailureBehavior() should change the behavior when failures occur', () => { | ||
allow.setFailureBehavior('ignore'); | ||
expect(allow.aString(aFunction)).toEqual(expect.any(Object)); | ||
allow.setFailureBehavior('throw'); | ||
expect(() => allow.aString(aFunction)).toThrow(); | ||
allow.setFailureBehavior('warn'); | ||
const originalWarningTotal = totalConsoleWarnings; | ||
expect(allow.aString(aFunction)).toEqual(expect.any(Object)); | ||
expect(totalConsoleWarnings).toBeGreaterThan(originalWarningTotal); | ||
allow.setFailureBehavior('throw'); | ||
expect(() => allow.aString(aFunction)).toThrow(); | ||
}); | ||
test('setFailureBehavior() should throw an error when supplied any value other than ignore, throw, or warn', () => { | ||
expect(() => allow.setFailureBehavior(aFalseString)).toThrow(); | ||
expect(() => allow.setFailureBehavior(aFunction)).toThrow(); | ||
expect(() => allow.setFailureBehavior(anArrayOfArrays)).toThrow(); | ||
expect(() => allow.setFailureBehavior(anArrayOfIntegers)).toThrow(); | ||
expect(() => allow.setFailureBehavior(anArrayOfMixedTypes)).toThrow(); | ||
expect(() => allow.setFailureBehavior(anArrayOfNumbers)).toThrow(); | ||
expect(() => allow.setFailureBehavior(anArrayOfObjects)).toThrow(); | ||
expect(() => allow.setFailureBehavior(anArrayOfPeople)).toThrow(); | ||
expect(() => allow.setFailureBehavior(anArrayOfStrings)).toThrow(); | ||
expect(() => allow.setFailureBehavior(aNegative1)).toThrow(); | ||
expect(() => allow.setFailureBehavior(aNegativePi)).toThrow(); | ||
expect(() => allow.setFailureBehavior(anEmptyArray)).toThrow(); | ||
expect(() => allow.setFailureBehavior(anEmptyObject)).toThrow(); | ||
expect(() => allow.setFailureBehavior(anEmptyString)).toThrow(); | ||
expect(() => allow.setFailureBehavior(aNull)).toThrow(); | ||
expect(() => allow.setFailureBehavior(aNumber0)).toThrow(); | ||
expect(() => allow.setFailureBehavior(aNumber1)).toThrow(); | ||
expect(() => allow.setFailureBehavior(aNumber1WithDecimals)).toThrow(); | ||
expect(() => allow.setFailureBehavior(anUndefined)).toThrow(); | ||
expect(() => allow.setFailureBehavior(aPi)).toThrow(); | ||
expect(() => allow.setFailureBehavior(aPopulatedObject)).toThrow(); | ||
expect(() => allow.setFailureBehavior(aTrueString)).toThrow(); | ||
}); | ||
// setOnFailure() | ||
test('the function supplied in setOnFailure() should be called on any failure', () => { | ||
allow.setOnFailure(customOnFailure); | ||
let originalOnFailureInvokations = totalOnFailureInvokations; | ||
expect(() => allow.aString(aFunction)).toThrow(); | ||
expect(totalOnFailureInvokations).toBeGreaterThan(originalOnFailureInvokations); | ||
allow.setOnFailure(() => { | ||
}); | ||
originalOnFailureInvokations = totalOnFailureInvokations; | ||
expect(() => allow.aString(aFunction)).toThrow(); | ||
expect(totalOnFailureInvokations).toEqual(originalOnFailureInvokations); | ||
}); | ||
test('setOnFailure() should throw an error when supplied any value other than a function', () => { | ||
expect(() => allow.setOnFailure(aFalse)).toThrow(); | ||
expect(() => allow.setOnFailure(aFalseString)).toThrow(); | ||
expect(() => allow.setOnFailure(anArrayOfArrays)).toThrow(); | ||
expect(() => allow.setOnFailure(anArrayOfIntegers)).toThrow(); | ||
expect(() => allow.setOnFailure(anArrayOfMixedTypes)).toThrow(); | ||
expect(() => allow.setOnFailure(anArrayOfNumbers)).toThrow(); | ||
expect(() => allow.setOnFailure(anArrayOfObjects)).toThrow(); | ||
expect(() => allow.setOnFailure(anArrayOfPeople)).toThrow(); | ||
expect(() => allow.setOnFailure(anArrayOfStrings)).toThrow(); | ||
expect(() => allow.setOnFailure(aNegative1)).toThrow(); | ||
expect(() => allow.setOnFailure(aNegativePi)).toThrow(); | ||
expect(() => allow.setOnFailure(anEmptyArray)).toThrow(); | ||
expect(() => allow.setOnFailure(anEmptyObject)).toThrow(); | ||
expect(() => allow.setOnFailure(anEmptyString)).toThrow(); | ||
expect(() => allow.setOnFailure(aNull)).toThrow(); | ||
expect(() => allow.setOnFailure(aNumber0)).toThrow(); | ||
expect(() => allow.setOnFailure(aNumber1)).toThrow(); | ||
expect(() => allow.setOnFailure(aNumber1WithDecimals)).toThrow(); | ||
expect(() => allow.setOnFailure(anUndefined)).toThrow(); | ||
expect(() => allow.setOnFailure(aPi)).toThrow(); | ||
expect(() => allow.setOnFailure(aPopulatedObject)).toThrow(); | ||
expect(() => allow.setOnFailure(aTrue)).toThrow(); | ||
expect(() => allow.setOnFailure(aTrueString)).toThrow(); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
82316
1125
0
664
7