@bemoje/is
v3.0.0
Published
Type checking and validation utilities with composable validators and detailed error reporting.
Downloads
188
Maintainers
Readme
@bemoje/is
Type checking and validation utilities with composable validators and detailed error reporting.
Exports
- IsArrayWhereEach: Creates a validator function that checks whether the input is an array where all elements are valid according to every validator provided.
- IsFileExt.
- IsLength: Creates a function that validates if the length of the input is equal to the specified length. The returned function accepts any value with a 'length' property and is named 'isLen' concatenated with the specified length.
- ValidatorError: Custom error class for validation failures, providing detailed information about the input, expected outcome, and cause of failure.
- createGtValidator: Creates a validator function that checks if a value is a number greater than the specified limit.
- createGteValidator: Creates a validator function that checks if a value is a number greater than or equal to the specified limit.
- createLtValidator: Creates a validator function that checks if a value is a number less than the specified limit.
- createLteValidator: Creates a validator function that checks if a value is a number less than or equal to the specified limit.
- ensureThat. If validation fails, an error is thrown with details about the failure. Validators can return strings indicating the reason for failure, which will be included in the error message.
- isArray: Checks if the provided value is an array.
- isChar: Determines whether a string is a single character.
- isClass: Checks if the given value is a constructor function using 'class' syntax. WARNING: If the running code is minified or mangled, this function may not work as expected. However, it should be resistant to minification/mangling if the 'class' keyword is present in the first line of the function.
- isConstructor: Checks if the given value is a valid constructor function.
- isDefined: This function checks if a value is defined or not. It performs a strict comparison against
undefined. - isDigit: Returns true if the given character is a digit between 0 and 9.
- isDigits: Returns true if the given string is a string of digits between 0 and 9.
- isEven: Checks if a number is even.
- isHex: Checks if a string is a hexadecimal number. Understands prefixes for hex colors, hex decimal and regexp unicode hex.
- isHexOrUnicode: Checks if a given string is a hexadecimal or unicode.
- isIntRange: Determine whether the input is an array of two integers in ascending order.
- isInteger: Checks if the provided number is an integer.
- isLen2: Determine whether the input has length of 2.
- isNamedFunction: Checks if the provided value is a named function.
- isNamedFunctionArray: Checks if the provided value is an array containing only named functions.
- isNegativeInteger: Checks if a given number is a negative integer.
- isNegativeNumber: Checks if a given number is negative or zero.
- isNonZeroNegativeInteger: Checks if a given number is a negative non-zero integer.
- isNonZeroNegativeNumber: Checks if a given value is a negative number less than zero.
- isNonZeroPositiveInteger: Checks if a given number is a positive non-zero integer.
- isNonZeroPositiveNumber: Checks if a given value is a positive number greater than zero.
- isNumArrayAscending: Determine whether the input is an array of numbers in ascending order. Duplicate values are allowed.
- isNumericString: Checks if a given string is numeric.
- isObject.
- isObjectType.
- isOdd: Checks if a number is odd.
- isPosIntArray integer array.
- isPosIntRange: Checks if the input is an array of exactly two positive integers in ascending order, representing a valid range.
- isPositiveInteger: Checks if a given number is a positive integer.
- isPositiveNumber.
- isPrototype: Checks if the given value is a prototype object
- isStringArray: Determine whether the input is a string array.
- isStringWithNoSpacesOrDashes: Checks if the provided value is a string that contains no spaces or dashes.
- isUniqueNumArrayAscending: Determine whether the input is an array of numbers in ascending order. Duplicate values are not allowed.
- isValidNumber.
Installation
npm install @bemoje/isUsage
Basic Type Guards
import { isArray, isInteger, isObject, isPositiveNumber, isEven } from '@bemoje/is'
isArray([1, 2, 3]) // true
isInteger(42) // true
isObject({ a: 1 }) // true
isPositiveNumber(-5) // false
isEven(4) // trueValidate with ensureThat
Assert that values pass validation, throwing detailed errors on failure:
import { ensureThat, isInteger, isPositiveNumber } from '@bemoje/is'
ensureThat(42, isInteger)
// 42
ensureThat(-3, [isInteger, isPositiveNumber])
// throws ValidatorError with cause: { isPositiveNumber: false }Create Composite Validators
import { IsArrayWhereEach, isInteger, isPositiveNumber } from '@bemoje/is'
const isPosIntArray = IsArrayWhereEach([isInteger, isPositiveNumber])
isPosIntArray([1, 2, 3]) // true
isPosIntArray([1, -2, 3]) // false
isPosIntArray('hello') // falseLength Validation
import { IsLength } from '@bemoje/is'
const isLen3 = IsLength(3)
isLen3([1, 2, 3]) // true
isLen3('abc') // true
isLen3([1, 2]) // falseFile Extension Validation
import { IsFileExt } from '@bemoje/is'
const isTs = IsFileExt('ts')
isTs('index.ts') // true
isTs('index.js') // falseComparison Validators
import { createGtValidator, createLteValidator } from '@bemoje/is'
const isGt10 = createGtValidator(10)
isGt10(15) // true
isGt10(5) // false
const isLte100 = createLteValidator(100)
isLte100(100) // true
isLte100(101) // falseValidation Error Details
import { ValidatorError } from '@bemoje/is'
try {
ensureThat(-3, [isInteger, isPositiveNumber])
} catch (e) {
if (e instanceof ValidatorError) {
console.log(e.input) // -3
console.log(e.expected) // true
console.log(e.cause) // { isPositiveNumber: false }
}
}