最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Joi decimal, less than 8 decimal places - Stack Overflow

programmeradmin0浏览0评论

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',
    })),

4,23443234 - valid data

4,234432341 - invalid data

Share Improve this question asked Jan 14, 2019 at 14:09 DemionDemion 571 silver badge9 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 9

You 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});
发布评论

评论列表(0)

  1. 暂无评论