JSON模式中的小数精度

编程入门 行业动态 更新时间:2024-10-22 16:38:18
本文介绍了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模式中的小数精度

本文发布于:2023-11-23 01:30:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619627.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小数   精度   模式   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!