OptimumCode/json-schema-validator

Bug: multipleOf round error

Closed this issue ยท 4 comments

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()

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

The fix is available in the latest SNAPSHOT version (it will be published in 15-20 minutes I think). I will publish a release once I finish the first part for #54.
Once again, thank you for reporting this issue @plastiv

Fixed in version: 0.0.9

Thank you for the quick resolution! I have checked 0.0.9 and confirm it fixed my issue.