Bug: multipleOf round error
plastiv opened this issue · comments
Sergii Pechenizkyi commented
Is there an existing issue for this?
- I have searched the existing issues
Current Behavior
ValidationError: 19.99 is not a multiple of 0.01
Expected Behavior
Validation shall pass. Note: values 9.99
, 29.99
are passing. It's only 19.99 has an issue.
JSON schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "multipleOf round error reproducer",
"required": ["price"],
"type": "object",
"properties": {
"price": {
"description": "Product price",
"type": "number",
"minimum": 0,
"multipleOf": 0.01
}
}
}
Library version
0.0.8
Anything else?
Full reproducer
val jsonStr = """
{
"price": 19.99
}
""".trimIndent()
val schema = """
{
"${"$"}schema": "http://json-schema.org/draft-07/schema#",
"description": "multipleOf round error reproducer",
"required": ["price"],
"type": "object",
"properties": {
"price": {
"description": "Product price",
"type": "number",
"minimum": 0,
"multipleOf": 0.01
}
}
}
""".trimIndent()
val jsonSchema = JsonSchema.fromDefinition(schema)
val jsonElement = Json.parseToJsonElement(jsonStr)
val errors = mutableListOf<ValidationError>()
jsonSchema.validate(jsonElement, errors::add)
assertThat(errors).isEmpty()
Oleg Smirnov commented
Hi, @plastiv. Thank you for reporting this issue. I will have a closer look at it today/tomorrow. It looks like something went wrong with rounding during the check
jshell> 19.99 * 100
$2 ==> 1998.9999999999998
jshell> 9.99 * 100
$3 ==> 999.0
jshell> 29.99 * 100
$4 ==> 2999.0
Oleg Smirnov commented
Oleg Smirnov commented
Fixed in version: 0.0.9
Sergii Pechenizkyi commented
Thank you for the quick resolution! I have checked 0.0.9 and confirm it fixed my issue.