checkRange
The checkRange() method validates whether a value is within a specified range, inclusive of the bounds. Unlike checkOptions(), this method expects an object for the range definition, with properties from and to.
It works with numeric values, BigInt (123n), Infinity, -Infinity, and can also be used to validate dates (via getTime()) or hours (via getHours()).
Usage
Validate.checkRange(value, { from, to });Parameters
value(number | bigint): The value to validate. It can be:A number (including
Infinityand-Infinity),A BigInt (
123n),A timestamp (e.g., from
Date.getTime()),An hour of the day (e.g., from
Date.getHours()).
range(object): An object containing two properties:from(number | bigint): The minimum value of the range.to(number | bigint): The maximum value of the range.
Return Value
Returns
trueif the value is within the specified range (inclusive).Returns
falseif the value is outside the range.
Examples
Valid cases with numeric values:
Valid cases with BigInt values:
Valid cases with Infinity:
Valid cases with Dates timestamps (getTime()):
getTime()):Valid cases with Hours values (getHours()):
getHours()):Valid cases with String values:
Invalid cases:
Limitations
The library checks for valid input types before performing any range checks.
Only numbers and BigInt values (including
Infinityand-Infinity) are accepted.Non-numeric values will be rejected immediately with an error.
Error Messages (Silent Mode Off)
If silent mode is disabled (Validate.silent(false)) and the value is outside the range, a descriptive error will be thrown:
Last updated