MQTTSuite
Loading...
Searching...
No Matches
issue-189-default-values.cpp File Reference
#include <iostream>
#include <nlohmann/json-schema.hpp>
Include dependency graph for issue-189-default-values.cpp:

Go to the source code of this file.

Functions

static void loader (const json_uri &uri, json &schema)
int main (void)

Variables

static const json rectangle_schema
static const json quad_schema
static const json default_schema

Function Documentation

◆ loader()

void loader ( const json_uri & uri,
json & schema )
static

Definition at line 68 of file issue-189-default-values.cpp.

69{
71}
auto schema
Definition id-ref.cpp:69
static const json default_schema

References default_schema.

Referenced by main().

Here is the caller graph for this function:

◆ main()

int main ( void )

Definition at line 73 of file issue-189-default-values.cpp.

74{
76
77 validator.set_root_schema(quad_schema);
78
79 {
80 json empty_quad = R"({})"_json;
81
82 const auto default_patch = validator.validate(empty_quad);
83 const auto actual = empty_quad.patch(default_patch);
84
85 const auto expected = R"({"height":10,"width":20,"depth":5,"time":15})"_json;
86 if (actual != expected) {
87 std::cerr << "Patch with defaults contains wrong value: '" << actual << "' instead of expected '" << expected.dump() << "'" << std::endl;
88 return 1;
89 }
90 }
91
92 validator.set_root_schema(rectangle_schema);
93
94 {
95 json empty_rectangle = R"({})"_json;
96
97 const auto default_patch = validator.validate(empty_rectangle);
98 const auto actual = empty_rectangle.patch(default_patch);
99
100 // height must be 10 according to the default specified in the length definition while width must be 10 overridden by the width element
101 const auto expected = R"({"height":10,"width":20})"_json;
102 if (actual != expected) {
103 std::cerr << "Patch with defaults contains wrong value: '" << actual << "' instead of expected '" << expected.dump() << "'" << std::endl;
104 return 1;
105 }
106 }
107
108 return 0;
109}
nlohmann::json_schema::json_validator validator
nlohmann::json json
static const json quad_schema
static const json rectangle_schema
static void loader(const json_uri &uri, json &schema)

References nlohmann::json_schema::json_validator::json_validator(), loader(), quad_schema, rectangle_schema, nlohmann::json_schema::json_validator::set_root_schema(), and nlohmann::json_schema::json_validator::validate().

Here is the call graph for this function:

Variable Documentation

◆ default_schema

const json default_schema
static
Initial value:
= R"(
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"defaultLength": {
"default": 5
}
}
})"_json

Definition at line 58 of file issue-189-default-values.cpp.

Referenced by loader().

◆ quad_schema

const json quad_schema
static
Initial value:
= R"(
{
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"width": {
"$ref": "#/properties/height",
"default": 20
},
"height": {
"$ref": "#/definitions/length"
},
"depth": {
"$ref": "default_schema#/definitions/defaultLength"
},
"time": {
"$ref": "#/definitions/time"
}
},
"definitions": {
"length": {
"$ref": "default_schema#/definitions/defaultLength",
"default": 10
},
"time": {
"type": "integer",
"default": 15
}
}
})"_json

Definition at line 28 of file issue-189-default-values.cpp.

Referenced by main().

◆ rectangle_schema

const json rectangle_schema
static
Initial value:
= R"(
{
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"width": {
"$ref": "#/definitions/length",
"default": 20
},
"height": {
"$ref": "#/definitions/length"
}
},
"definitions": {
"length": {
"type": "integer",
"default": 10
}
}
})"_json

Definition at line 8 of file issue-189-default-values.cpp.

Referenced by main().