2017-02-27 51 views
2

我正在使用JSON Schema驗證服務器請求,並且我有一些值希望驗證爲2DP。我用下面的架構來驗證這些字段:JSON Schema給我multipleOf 0.01的驗證錯誤以.49或.99結尾的任何數字

'properties': { 
    'amount': {'type': ['number', 'null'], 'multipleOf': 0.01} 
} 

這工作正常比.49或.99,在那裏我得到的錯誤amount is not a multiple of (divisible by) 0.01結尾號碼之外的所有情況。

這可能是某種浮點錯誤。如果這是不正確的,我應該如何驗證數字到一定的精度?

+0

我不得不定義選項multipleOfPrecision在ajv https://github.com/epoberezkin/ajv – esp

回答

0

根據Issues #185您需要將您的數字轉換爲小數以防止浮點算術問題。

2

爲了避免通過循環和鑄造的小數位以上的建議,我最後寫一個自定義的驗證:

Validator.prototype.customFormats.currency = function(input) { 
    if (input === undefined) { return true} 
    return (input * 100) % 1 === 0 
}