Need your help in setting up the library schema for Joi. Task, you need to do validation if the number of characters exceeds 8 characters after mas. I did it using custom validation, but I would like to use Joi use everywhere in the project:
myField: Joi.number()
.positive()
.less(999999999999)
.required()
.error(() => ({
message: 'Error',
})),
4,23443234 - valid data
4,234432341 - invalid data
Need your help in setting up the library schema for Joi. Task, you need to do validation if the number of characters exceeds 8 characters after mas. I did it using custom validation, but I would like to use Joi use everywhere in the project:
myField: Joi.number()
.positive()
.less(999999999999)
.required()
.error(() => ({
message: 'Error',
})),
Share Improve this question asked Jan 14, 2019 at 14:09 DemionDemion 571 silver badge9 bronze badges 04,23443234 - valid data
4,234432341 - invalid data
1 Answer
Reset to default 9You can find the solution by reading Joi's documentation:
number.precision(limit)
Specifies the maximum number of decimal places where:
limit
- the maximum number of decimal places allowed.const schema = Joi.number().precision(2);
Using precision
will by default just round the decimals to the specified maximum. If you want it to fail the validation instead, set convert
to false
as per the documentation:
joi.validate(objectToValidate, schema, {convert:false});