joi-decimal
Joi extension for Decimal type.
Useful to validate any scientific / financial number.
⚠️ do not use .precision()
for now as it doesn't work as expected.
It will be fixed in v1.2.0.
Usage
const BaseJoi = ;const DecimalExtension = ;const Joi = BaseJoi; const schema = Joi; const result = schema;// result.error === null -> valid
API
decimal
- inherits from Any
Generates a schema object that matches a Decimal type (as well as a JavaScript string or number that can be converted to Decimal type). If
the validation convert
option is on (enabled by default), a string or number will be converted to a Decimal
, if specified.
Also, if
convert
is on and decimal.precision()
is used, the value will be converted to the specified precision
as well.
const dec = Joi;dec;
Possible validation errors: decimal.base
decimal.finite()
Requires the number to be finite.
const schema = Joi;
Possible validation errors: decimal.finite
decimal.greater(limit)
Specifies that the value must be greater than limit
or a reference.
const schema = Joi;
const schema = Joiobject min: Joi max: Joi;
Possible validation errors: decimal.greater
, decimal.ref
decimal.integer()
Requires the number to be an integer (no floating point).
const schema = Joi;
Possible validation errors: decimal.integer
decimal.less(limit)
Specifies that the value must be less than limit
or a reference.
const schema = Joi;
const schema = Joiobject min: Joi max: Joi;
Possible validation errors: decimal.less
, decimal.ref
decimal.max(limit)
Specifies the maximum value where:
limit
- the maximum value allowed or a reference.
const schema = Joi;
const schema = Joiobject min: Joi max: Joi;
Possible validation errors: decimal.max
, decimal.ref
decimal.min(limit)
Specifies the minimum value where:
limit
- the minimum value allowed or a reference.
const schema = Joi;
const schema = Joiobject min: Joi max: Joi;
Possible validation errors: decimal.min
, decimal.ref
decimal.multiple(base)
Specifies that the value must be a multiple of base
(or a reference):
const schema = Joimultiple3;
Possible validation errors: decimal.multiple
, decimal.ref
decimal.nan()
Requires the number to be NaN.
const schema = Joi;
Possible validation errors: decimal.nan
decimal.negative()
Requires the number to be negative.
const schema = Joi;
Possible validation errors: decimal.negative
decimal.positive()
Requires the number to be positive.
const schema = Joi;
Possible validation errors: decimal.positive
decimal.precision(sd, rm)
Specifies the maximum precision where:
sd
- the number of significant digits on which to round.rm
- the rounding mode to use.
const schema = Joi;
Possible validation errors: decimal.precision
decimal.zero()
Requires the number to be zero.
const schema = Joi;
Possible validation errors: decimal.zero
List of errors
decimal.base
The value is not a Decimal or could not be cast to a Decimal.
decimal.finite
The number was not finite.
decimal.greater
The number is lower or equal to the limit that you set.
Additional local context properties:
decimal.integer
The number is not a valid integer.
decimal.less
The number is higher or equal to the limit that you set.
Additional local context properties:
decimal.max
The number is higher than the limit that you set.
Additional local context properties:
decimal.min
The number is lower than the limit that you set.
Additional local context properties:
decimal.multiple
The number could not be divided by the base you provided.
Additional local context properties:
decimal.nan
The number is not NaN.
decimal.negative
The number was positive.
decimal.only
Only some values were allowed, the input didn't match any of them.
Additional local context properties:
decimal.positive
The number was negative.
decimal.precision
The arguments (sd
and/or rm
) are not numbers.
decimal.ref
A reference was used in one of decimal.greater()
, decimal.less()
, decimal.max()
, decimal.min()
or decimal.multiple()
and the value pointed to by that reference in the input is not a valid Decimal or could not be cast to a Decimal.
decimal.zero
The number is not zero.