[英]Joi decimal, less than 8 decimal places
Need your help in setting up the library schema for Joi.在为 Joi 设置库模式时需要您的帮助。 Task, you need to do validation if the number of characters exceeds 8 characters after commas.
任务,如果逗号后的字符数超过8个字符,则需要进行验证。 I did it using custom validation, but I would like to use Joi use everywhere in the project:
我是使用自定义验证来完成的,但我想在项目中的任何地方使用 Joi 使用:
myField: Joi.number()
.positive()
.less(999999999999)
.required()
.error(() => ({
message: 'Error',
})),
4,23443234 - valid data
4,23443234 - 有效数据
4,234432341 - invalid data
4,234432341 - 无效数据
You can find the solution by reading Joi's documentation :您可以通过阅读Joi 的文档找到解决方案:
number.precision(limit)
Specifies the maximum number of decimal places where:
指定最大小数位数,其中:
limit
- the maximum number of decimal places allowed.limit
- 允许的最大小数位数。const schema = Joi.number().precision(2);
Using precision
will by default just round the decimals to the specified maximum.默认情况下,使用
precision
只会将小数四舍五入到指定的最大值。 If you want it to fail the validation instead, set convert
to false
as per the documentation :如果您希望它无法通过验证,请根据文档
convert
设置为false
:
joi.validate(objectToValidate, schema, {convert:false});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.