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 74931bd
Show file tree
Hide file tree
Showing 3 changed files with 59 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)
41 changes: 41 additions & 0 deletions test/issue-255-error-message-limit-precision.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <nlohmann/json-schema.hpp>
#include <stdexcept>

using nlohmann::json;
using nlohmann::json_schema::json_validator;

static const json schema = R"(
{
"$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
}
}
})"_json;

class custom_error_handler : public nlohmann::json_schema::basic_error_handler
{
void error(const nlohmann::json::json_pointer &ptr, const json &instance, const std::string &message) override
{
if (message != "instance exceeds maximum of 3.141592653589793")
throw std::invalid_argument("Precision print does not work.");
}
};

int main(void)
{
json_validator validator;

auto instance = R"({ "angle": 3.1415927410125732 })"_json;

validator.set_root_schema(schema);
custom_error_handler err;
validator.validate(instance, err);

return 0;
}

2 comments on commit 74931bd

@LecrisUT
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you forgot to run pre-commit on this one

@pboettch
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bad person. 😢

Please sign in to comment.