Go to the documentation of this file.
4#include <nlohmann/json-schema.hpp>
11{
12 "$schema": "http://json-schema.org/draft-07/schema#",
13 "title": "A person",
14 "properties": {
15 "name": {
16 "description": "Name",
17 "type": "string"
18 },
19 "age": {
20 "description": "Age of the person",
21 "type": "number",
22 "minimum": 2,
23 "maximum": 200
24 },
25 "address":{
26 "type": "object",
27 "properties":{
28 "street":{
29 "type": "string",
30 "default": "Abbey Road"
31 }
32 }
33 }
34 },
35 "required": [
36 "name",
37 "age"
38 ],
39 "type": "object"
40}
41
42)"_json;
46static json
good_person = {{
"name",
"Albert"}, {
"age", 42}, {
"address", {{
"street",
"Main Street"}}}};
57 }
catch (
const std::exception &e) {
58 std::cerr <<
"Validation of schema failed, here is why: " << e.what() <<
"\n";
65 std::cout <<
"About to validate this person:\n"
66 << std::setw(2) << person << std::endl;
69 std::cout <<
"Validation succeeded\n";
70 std::cout <<
"Patch with defaults: " << defaultPatch.dump(2) << std::endl;
71 }
catch (
const std::exception &e) {
72 std::cerr <<
"Validation failed, here is why: " << e.what() <<
"\n";
79 void error(
const nlohmann::json::json_pointer &ptr,
const json &instance,
const std::string &message)
override
82 std::cerr <<
"ERROR: '" << ptr <<
"' - '" << instance <<
"': " << message <<
"\n";
87 std::cout <<
"About to validate this person:\n"
88 << std::setw(2) << person << std::endl;
90 custom_error_handler err;
94 std::cerr <<
"Validation failed\n";
96 std::cout <<
"Validation succeeded\n";
void error(const json::json_pointer &, const json &, const std::string &) override
void set_root_schema(const json &)
json validate(const json &, error_handler &, const json_uri &initial_uri=json_uri("#")) const
json validate(const json &) const
static json person_schema
static json good_defaulted_person