JSON模式中的小数精度 [英] Decimal precision in JSON Schema

查看:745
本文介绍了JSON模式中的小数精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的JSON模式验证是否将不超过两个小数位发送到我的REST api.

I want to have my JSON Schema validate that no more than two decimal places are sent to my REST api.

根据我在最新的JSON模式RFC(v4)中看到的内容,不允许这样做. V1有一个maxDecimals验证器.

From what I can see in the latest JSON Schema RFC (v4) doesn't allow this. V1 had a maxDecimals validator.

有人知道为什么要这么做吗?

Does anyone know why that was taken out?

我有一个字段,当我将其存储在数据库中时,该字段仅包含两位小数,并且我不只是想舍入到两位小数.对于某些用户而言,那将极大地改变输入.因此,我想拒绝任何更高的精度,并强迫他们四舍五入.

I have a field that only holds two decimals when I store it in the database, and I do not just want to round down to two decimals. That would be changing the input quite dramatically for some users. So I want to reject any greater precision and force them to round them selves.

我当然可以使用我自己编写的自定义验证程序来执行此操作,但除非绝对必要,否则我不愿意这样做.

I can of course do this using a custom validator that I write myself, but I would rather not unless I absolutely have to.

在v4中还有另一种指示方式吗?

Is there another way of indicating this in v4?

谢谢

推荐答案

他们将其替换为multipleOf(通过v3 divisibleBy).

They replaced it with multipleOf (via v3 divisibleBy).

对于2个小数位,只需添加倍数Of:0.01.

For 2 decimal places, just add multipleOf: 0.01.

这篇关于JSON模式中的小数精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆