@@ -5,28 +5,52 @@ using nlohmann::json;
5
5
using nlohmann::json_uri;
6
6
using nlohmann::json_schema::json_validator;
7
7
8
- static const json quad_schema = R"(
8
+ static const json rectangle_schema = R"(
9
9
{
10
10
"$schema": "http://json-schema.org/draft-07/schema#",
11
11
"properties": {
12
12
"width": {
13
13
"$ref": "#/definitions/length",
14
14
"default": 20
15
15
},
16
+ "height": {
17
+ "$ref": "#/definitions/length"
18
+ }
19
+ },
20
+ "definitions": {
21
+ "length": {
22
+ "type": "integer",
23
+ "default": 10
24
+ }
25
+ }
26
+ })" _json;
27
+
28
+ static const json quad_schema = R"(
29
+ {
30
+ "$schema": "http://json-schema.org/draft-07/schema#",
31
+ "properties": {
32
+ "width": {
33
+ "$ref": "#/properties/height",
34
+ "default": 20
35
+ },
16
36
"height": {
17
37
"$ref": "#/definitions/length"
18
38
},
19
39
"depth": {
20
40
"$ref": "default_schema#/definitions/defaultLength"
21
41
},
22
42
"time": {
23
- "$ref": "#/properties/width "
43
+ "$ref": "#/definitions/time "
24
44
}
25
45
},
26
46
"definitions": {
27
47
"length": {
28
48
"$ref": "default_schema#/definitions/defaultLength",
29
49
"default": 10
50
+ },
51
+ "time": {
52
+ "type": "integer",
53
+ "default": 15
30
54
}
31
55
}
32
56
})" _json;
@@ -52,14 +76,29 @@ int main(void)
52
76
53
77
validator.set_root_schema (quad_schema);
54
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
+
55
94
{
56
95
json empty_rectangle = R"( {})" _json;
57
96
58
97
const auto default_patch = validator.validate (empty_rectangle);
59
98
const auto actual = empty_rectangle.patch (default_patch);
60
99
61
100
// height must be 10 according to the default specified in the length definition while width must be 10 overridden by the width element
62
- const auto expected = R"( {"height":10,"width":20,"depth":5,"time":20 })" _json;
101
+ const auto expected = R"( {"height":10,"width":20})" _json;
63
102
if (actual != expected) {
64
103
std::cerr << " Patch with defaults contains wrong value: '" << actual << " ' instead of expected '" << expected.dump () << " '" << std::endl;
65
104
return 1 ;
0 commit comments