Skip to content

Commit

Permalink
error-messages: Numeric limit errors should show maximum precision
Browse files Browse the repository at this point in the history
Fixes #255
  • Loading branch information
pboettch committed Nov 27, 2023
1 parent 3f6376d commit 90a4b3a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/json-validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -877,22 +877,31 @@ class numeric : public schema
{
T value = instance; // conversion of json to value_type

std::ostringstream oss;

if (multipleOf_.first && value != 0) // zero is multiple of everything
if (violates_multiple_of(value))
e.error(ptr, instance, "instance is not a multiple of " + std::to_string(multipleOf_.second));
oss << "instance is not a multiple of " << json(multipleOf_.second);

if (maximum_.first) {
if (exclusiveMaximum_ && value >= maximum_.second)
e.error(ptr, instance, "instance exceeds or equals maximum of " + std::to_string(maximum_.second));
oss << "instance exceeds or equals maximum of " << json(maximum_.second);
else if (value > maximum_.second)
e.error(ptr, instance, "instance exceeds maximum of " + std::to_string(maximum_.second));
oss << "instance exceeds maximum of " << json(maximum_.second);
}

if (minimum_.first) {
if (exclusiveMinimum_ && value <= minimum_.second)
e.error(ptr, instance, "instance is below or equals minimum of " + std::to_string(minimum_.second));
oss << "instance is below or equals minimum of " << json(minimum_.second);
else if (value < minimum_.second)
e.error(ptr, instance, "instance is below minimum of " + std::to_string(minimum_.second));
oss << "instance is below minimum of " << json(minimum_.second);
}

oss.seekp(0, std::ios::end);
auto size = oss.tellp();
if (size != 0) {
oss.seekp(0, std::ios::beg);
e.error(ptr, instance, oss.str());
}
}

Expand Down
4 changes: 4 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,7 @@ add_test(NAME issue-229-oneof-default-values COMMAND issue-229-oneof-default-val
add_executable(issue-243-root-default-values issue-243-root-default-values.cpp)
target_link_libraries(issue-243-root-default-values nlohmann_json_schema_validator)
add_test(NAME issue-243-root-default-values COMMAND issue-243-root-default-values)

add_executable(issue-255-error-message-limit-precision issue-255-error-message-limit-precision.cpp)
target_link_libraries(issue-255-error-message-limit-precision nlohmann_json_schema_validator)
add_test(NAME issue-255-error-message-limit-precision COMMAND issue-255-error-message-limit-precision)

0 comments on commit 90a4b3a

Please sign in to comment.