Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: multipleOf round error #70

Closed
1 task done
plastiv opened this issue Feb 27, 2024 · 4 comments · Fixed by #71
Closed
1 task done

Bug: multipleOf round error #70

plastiv opened this issue Feb 27, 2024 · 4 comments · Fixed by #71
Assignees
Labels
bug Something isn't working

Comments

@plastiv
Copy link

plastiv commented Feb 27, 2024

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()
@plastiv plastiv added the bug Something isn't working label Feb 27, 2024
@OptimumCode
Copy link
Owner

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

@OptimumCode
Copy link
Owner

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

@OptimumCode
Copy link
Owner

Fixed in version: 0.0.9

@plastiv
Copy link
Author

plastiv commented Mar 1, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Development

Successfully merging a pull request may close this issue.

2 participants