Skip to content

Commit 74931bd

Browse files
committed
error-messages: Numeric limit errors should show maximum precision
Fixes #255
1 parent 3f6376d commit 74931bd

3 files changed

+59
-5
lines changed

src/json-validator.cpp

+14-5
Original file line numberDiff line numberDiff line change
@@ -877,22 +877,31 @@ class numeric : public schema
877877
{
878878
T value = instance; // conversion of json to value_type
879879

880+
std::ostringstream oss;
881+
880882
if (multipleOf_.first && value != 0) // zero is multiple of everything
881883
if (violates_multiple_of(value))
882-
e.error(ptr, instance, "instance is not a multiple of " + std::to_string(multipleOf_.second));
884+
oss << "instance is not a multiple of " << json(multipleOf_.second);
883885

884886
if (maximum_.first) {
885887
if (exclusiveMaximum_ && value >= maximum_.second)
886-
e.error(ptr, instance, "instance exceeds or equals maximum of " + std::to_string(maximum_.second));
888+
oss << "instance exceeds or equals maximum of " << json(maximum_.second);
887889
else if (value > maximum_.second)
888-
e.error(ptr, instance, "instance exceeds maximum of " + std::to_string(maximum_.second));
890+
oss << "instance exceeds maximum of " << json(maximum_.second);
889891
}
890892

891893
if (minimum_.first) {
892894
if (exclusiveMinimum_ && value <= minimum_.second)
893-
e.error(ptr, instance, "instance is below or equals minimum of " + std::to_string(minimum_.second));
895+
oss << "instance is below or equals minimum of " << json(minimum_.second);
894896
else if (value < minimum_.second)
895-
e.error(ptr, instance, "instance is below minimum of " + std::to_string(minimum_.second));
897+
oss << "instance is below minimum of " << json(minimum_.second);
898+
}
899+
900+
oss.seekp(0, std::ios::end);
901+
auto size = oss.tellp();
902+
if (size != 0) {
903+
oss.seekp(0, std::ios::beg);
904+
e.error(ptr, instance, oss.str());
896905
}
897906
}
898907

test/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,7 @@ add_test(NAME issue-229-oneof-default-values COMMAND issue-229-oneof-default-val
8585
add_executable(issue-243-root-default-values issue-243-root-default-values.cpp)
8686
target_link_libraries(issue-243-root-default-values nlohmann_json_schema_validator)
8787
add_test(NAME issue-243-root-default-values COMMAND issue-243-root-default-values)
88+
89+
add_executable(issue-255-error-message-limit-precision issue-255-error-message-limit-precision.cpp)
90+
target_link_libraries(issue-255-error-message-limit-precision nlohmann_json_schema_validator)
91+
add_test(NAME issue-255-error-message-limit-precision COMMAND issue-255-error-message-limit-precision)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <nlohmann/json-schema.hpp>
2+
#include <stdexcept>
3+
4+
using nlohmann::json;
5+
using nlohmann::json_schema::json_validator;
6+
7+
static const json schema = R"(
8+
{
9+
"$schema": "http://json-schema.org/draft-07/schema#",
10+
"$id": "arc.schema.json",
11+
"properties": {
12+
"angle": {
13+
"type": "number",
14+
"description": "Radians, from -π to π.",
15+
"minimum": -3.14159265358979323846,
16+
"maximum": 3.14159265358979323846
17+
}
18+
}
19+
})"_json;
20+
21+
class custom_error_handler : public nlohmann::json_schema::basic_error_handler
22+
{
23+
void error(const nlohmann::json::json_pointer &ptr, const json &instance, const std::string &message) override
24+
{
25+
if (message != "instance exceeds maximum of 3.141592653589793")
26+
throw std::invalid_argument("Precision print does not work.");
27+
}
28+
};
29+
30+
int main(void)
31+
{
32+
json_validator validator;
33+
34+
auto instance = R"({ "angle": 3.1415927410125732 })"_json;
35+
36+
validator.set_root_schema(schema);
37+
custom_error_handler err;
38+
validator.validate(instance, err);
39+
40+
return 0;
41+
}

0 commit comments

Comments
 (0)