JSON模式给我multipleOf 0.01validation错误的任何数字以.49或.99结尾
我正在使用JSON模式来validation服务器请求,我有一些值,我想validation2DP。 我已经使用下面的模式来validation这些字段:
'properties': { 'amount': {'type': ['number', 'null'], 'multipleOf': 0.01} }
这适用于以.49或.99结尾的数字以外的所有情况,其中我得到的错误amount is not a multiple of (divisible by) 0.01
整数amount is not a multiple of (divisible by) 0.01
。
这大概是某种浮点错误。 如果这是不正确的,我应该如何validation数字到一定的精度?
为了避免循环和铸造小数如上所述,我结束了写一个自定义validation器:
Validator.prototype.customFormats.currency = function(input) { if (input === undefined) { return true} return (input * 100) % 1 === 0 }
根据问题#185你需要把你的数字作为小数来防止浮点算术问题。