error-messages: Numeric limit errors should show maximum precision
Fixes #255
This commit is contained in:
parent
3f6376dd6a
commit
74931bd02a
@ -877,22 +877,31 @@ class numeric : public schema
|
|||||||
{
|
{
|
||||||
T value = instance; // conversion of json to value_type
|
T value = instance; // conversion of json to value_type
|
||||||
|
|
||||||
|
std::ostringstream oss;
|
||||||
|
|
||||||
if (multipleOf_.first && value != 0) // zero is multiple of everything
|
if (multipleOf_.first && value != 0) // zero is multiple of everything
|
||||||
if (violates_multiple_of(value))
|
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 (maximum_.first) {
|
||||||
if (exclusiveMaximum_ && value >= maximum_.second)
|
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)
|
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 (minimum_.first) {
|
||||||
if (exclusiveMinimum_ && value <= minimum_.second)
|
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)
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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)
|
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)
|
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_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
test/issue-255-error-message-limit-precision.cpp
Normal file
41
test/issue-255-error-message-limit-precision.cpp
Normal file
@ -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;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user