Joi extension for Decimal type.
Useful to validate any scientific / financial number.
.precision()
for now as it doesn't work as expected.
It will be fixed in v1.2.0.
const BaseJoi = require('@hapi/joi');
const DecimalExtension = require('joi-decimal');
const Joi = BaseJoi.extend(DecimalExtension);
const schema = Joi.decimal().greater(100.0);
const result = schema.validate(101.00);
// result.error === null -> valid
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.decimal();
dec.validate('0.046875', (err, value) => { });
Possible validation errors: decimal.base
Requires the number to be finite.
const schema = Joi.decimal().finite();
Possible validation errors: decimal.finite
Specifies that the value must be greater than limit
or a reference.
const schema = Joi.decimal().greater(5.000);
const schema = Joi.object({
min: Joi.decimal().required(),
max: Joi.decimal().greater(Joi.ref('min')).required()
});
Possible validation errors: decimal.greater
, decimal.ref
Requires the number to be an integer (no floating point).
const schema = Joi.decimal().integer();
Possible validation errors: decimal.integer
Specifies that the value must be less than limit
or a reference.
const schema = Joi.decimal().less(10);
const schema = Joi.object({
min: Joi.decimal().less(Joi.ref('max')).required(),
max: Joi.decimal().required()
});
Possible validation errors: decimal.less
, decimal.ref
Specifies the maximum value where:
limit
- the maximum value allowed or a reference.
const schema = Joi.decimal().max(10);
const schema = Joi.object({
min: Joi.decimal().max(Joi.ref('max')).required(),
max: Joi.decimal().required()
});
Possible validation errors: decimal.max
, decimal.ref
Specifies the minimum value where:
limit
- the minimum value allowed or a reference.
const schema = Joi.decimal().min(2);
const schema = Joi.object({
min: Joi.decimal().required(),
max: Joi.decimal().min(Joi.ref('min')).required()
});
Possible validation errors: decimal.min
, decimal.ref
Specifies that the value must be a multiple of base
(or a reference):
const schema = Joi.decimal().multiple(3);
Possible validation errors: decimal.multiple
, decimal.ref
Requires the number to be NaN.
const schema = Joi.decimal().nan();
Possible validation errors: decimal.nan
Requires the number to be negative.
const schema = Joi.decimal().negative();
Possible validation errors: decimal.negative
Requires the number to be positive.
const schema = Joi.decimal().positive();
Possible validation errors: decimal.positive
Specifies the maximum precision where:
sd
- the number of significant digits on which to round.rm
- the rounding mode to use.
const schema = Joi.decimal().precision(2, 5);
Possible validation errors: decimal.precision
Requires the number to be zero.
const schema = Joi.decimal().zero();
Possible validation errors: decimal.zero
The value is not a Decimal or could not be cast to a Decimal.
The number was not finite.
The number is lower or equal to the limit that you set.
Additional local context properties:
{
limit: number // Minimum value that was expected for this number
}
The number is not a valid integer.
The number is higher or equal to the limit that you set.
Additional local context properties:
{
limit: number // Maximum value that was expected for this number
}
The number is higher than the limit that you set.
Additional local context properties:
{
limit: number // Maximum value that was expected for this number
}
The number is lower than the limit that you set.
Additional local context properties:
{
limit: number // Minimum value that was expected for this number
}
The number could not be divided by the base you provided.
Additional local context properties:
{
base: number // The number of which the input is supposed to be a multiple of
}
The number is not NaN.
The number was positive.
The number was negative.
The arguments (sd
and/or rm
) are not numbers.
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.
The number is not zero.