pboettch / json-schema-validator

JSON schema validator for JSON for Modern C++
Other
466 stars 134 forks source link

Numeric limit errors should show maximum precision #255

Closed toojays closed 5 months ago

toojays commented 1 year ago

Consider the following schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "arc.schema.json",
  "properties": {
    "angle": {
      "type": "number",
      "description": "Radians, from -π to π.",
      "minimum": -3.14159265358979323846,
      "maximum": 3.14159265358979323846
    }
  }
}

An interesting case is where a 32-bit floating point representation of PI is converted to a double and serialised:

$ echo '{ "angle": 3.1415927410125732 }' | ./json-schema-validate /tmp/arc.schema.json 
ERROR: '/angle' - '3.1415927410125732': instance exceeds maximum of 3.141593
schema validation failed

The error message is confusing. The maximum has been truncated to six decimal places, which makes it look larger than the value it is complaining about. Can we use the same mechanism to print the maximum as is used to print the input value?

toojays commented 1 year ago

I should have mentioned, this is with the main branch as at cae6fad80.

pboettch commented 1 year ago

Can we use the same mechanism to print the maximum as is used to print the input value?

Yes, I don't see why not. Could you propose a patch and a test?