MQTTSuite
Loading...
Searching...
No Matches
issue-229-oneof-default-values.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <nlohmann/json-schema.hpp>
3
4using nlohmann::json;
5using nlohmann::json_uri;
6using nlohmann::json_schema::json_validator;
7
8static const json default_schema = R"(
9{
10 "$schema": "http://json-schema.org/draft-07/schema#",
11 "type": "object",
12 "oneOf": [
13 {
14 "type": "object",
15 "properties": {
16 "name": {
17 "enum": "foo"
18 },
19 "code": {
20 "const": 1,
21 "default": 1
22 }
23 }
24 },
25 {
26 "type": "object",
27 "properties": {
28 "name": {
29 "enum": "bar"
30 },
31 "code": {
32 "const": 2,
33 "default": 2
34 }
35 }
36 }
37 ]
38})"_json;
39
40static void loader(const json_uri &uri, json &schema)
41{
42 schema = default_schema;
43}
44
45int main(void)
46{
47 json_validator validator(loader);
48
50
51 json data = R"({"name": "bar"})"_json;
52 json expected = R"(
53 [
54 {
55 "op": "add",
56 "path": "/code",
57 "value": 2
58 }
59 ]
60 )"_json;
61
62 json patch = validator.validate(data);
63 if (patch != expected) {
64 std::cerr << "Patch contains wrong operation: '" << patch.dump() << "' instead of expected '" << expected.dump() << "'" << std::endl;
65 return 1;
66 }
67
68 return 0;
69}
json_validator(schema_loader=nullptr, format_checker=nullptr, content_checker=nullptr)
int main()
Definition format.cpp:34
static void loader(const json_uri &uri, json &schema)
static const json default_schema