-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
error-messages: Numeric limit errors should show maximum precision
Fixes #255
- Loading branch information
Showing
3 changed files
with
59 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
74931bd
There was a problem hiding this comment.
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
74931bd
There was a problem hiding this comment.
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. 😢