isNumberInRange
isNumberInRange({ min, max })
Verifies if provided Verificable
represents a finite number within specified range. Both min
and max
arguments are optional - not providing any of them means value on that 'end' will not be checked.
Arguments:
min
- optional, of typenumber
max
- optional, of typenumber
Possible errors:
If provided Verificable
is not a number:
Verificable
is not a number:{
type: "isNumber",
path: [/* ... */]
}
If provided Verificable
is not a finite number:
Verificable
is not a finite number:{
type: "isFiniteNumber",
path: [/* ... */]
}
If provided Verificable
is a number outside the range:
Verificable
is a number outside the range:{
type: "isNumberInRange",
path: [/* ... */],
min: /* provided min value */,
max: /* provided max value */,
}
Examples:
Each of examples uses this import statement:
const { asVerificable, isValid, isNumberInRange, getErrors } = require("verifica");
Null or undefined is not considered a number:
const verificable = asVerificable(null);
isValid(verificable, isNumberInRange({ min: 100, max: 200 })); // false
String value is not considered a number as well:
const verificable = asVerificable("123");
isValid(verificable, isNumberInRange({ min: 100, max: 200 })); // false
Not finite numbers will not match with isFiniteNumber
error:
isFiniteNumber
error:const verificable = asVerificable(Infinity);
isValid(verificable, isNumberInRange({ min: 100 })); // false
getErrors(verificable, isNumberInRange({ min: 100 }));
// [{ type: "isFiniteNumber", path: [] }]
Any finite number outside of requested range will fail with isNumberInRange
error:
isNumberInRange
error:const verificable = asVerificable(9999);
isValid(verificable, isNumberInRange({ min: 100, max: 200 })); // false
getErrors(verificable, isNumberInRange({ min: 100, max: 200 }));
// [{ type: "isNumberInRange", min: 100, max: 200, path: [] }]
Not providing min
, max
or both constrains will mean no requirement on those missing 'ends' of the range:
min
, max
or both constrains will mean no requirement on those missing 'ends' of the range:const verificable = asVerificable(9999);
isValid(verificable, isNumberInRange({ min: 100 })); // true
Last updated
Was this helpful?