File: make_json_schema.md

package info (click to toggle)
jsoncons 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,584 kB
  • sloc: cpp: 136,382; sh: 33; makefile: 5
file content (299 lines) | stat: -rw-r--r-- 8,380 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
### jsoncons::jsonschema::make_json_schema

```cpp
#include <jsoncons_ext/jsonschema/jsonschema.hpp>

template <typename Json>
json_schema<Json> make_json_schema(const Json& sch,                 (until 0.175.0)
    evaluation_options options = evaluation_options{});        
                                                               (1)
template <typename Json>
json_schema<Json> make_json_schema(Json sch,                        (since 0.175.0)
    evaluation_options options = evaluation_options{});        

template <typename Json,typename SchemaResolver>
json_schema<Json> make_json_schema(const Json& sch,                 (until 0.175.0)
    const SchemaResolver& resolver,                               
    evaluation_options options = evaluation_options{});        (2)

template <typename Json,typename SchemaResolver>
json_schema<Json> make_json_schema(Json sch,                        (since 0.175.0)
    const SchemaResolver& resolver,                               
    evaluation_options options = evaluation_options{});        

template <typename Json>
json_schema<Json> make_json_schema(const Json& sch,                 (until 0.175.0)
    const std::string& retrieval_uri,                          
    evaluation_options options = evaluation_options{});         
                                                               (3)
template <typename Json>
json_schema<Json> make_json_schema(Json sch,                        (since 0.175.0) 
    const std::string& retrieval_uri,                          
    evaluation_options options = evaluation_options{});         

template <typename Json,typename SchemaResolver>
json_schema<Json> make_json_schema(const Json& sch,                 (until 0.175.0)
    const std::string& retrieval_uri,                          
    const SchemaResolver& resolver, 
    evaluation_options options = evaluation_options{});         
                                                               (4)
template <typename Json,typename SchemaResolver>
json_schema<Json> make_json_schema(Json sch,                        (since 0.175.0) 
    const std::string& retrieval_uri,                          
    const SchemaResolver& resolver, 
    evaluation_options options = evaluation_options{});         
```

Returns a [json_schema<Json>](json_schema.md) that represents a compiled JSON Schema document.

#### Parameters

<table>
  <tr>
    <td>schema</td>
    <td>JSON Schema</td> 
  </tr>
  <tr>
    <td>resolver</td>
    <td>A function object with the signature of <code>resolver</code> being equivalent to 
    <pre>
    Json fun(const <a href="../corelib/utility/uri.md">jsoncons::uri</a>& uri);</pre>
    If unable to resolve the resource, it should return <code>Json::null()</code>.
    </td>   
  </tr>
  <tr>
    <td>retrieval_uri</td>
    <td>Optional retrieval URI</td> 
  </tr>
  <tr>
    <td><a href="evaluation_options.md">options</a></td>
    <td>Evaluation options</td> 
  </tr>
</table>

#### Return value

A [json_schema<Json>](json_schema.md) that represents a compiled JSON Schema document.

#### Exceptions

(1)-(4) Throws a [schema_error](schema_error.md) if JSON Schema compilation fails.

### Examples

#### Draft 2020-12 example (from the JSON Schema Test Suite)

```cpp
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonschema/jsonschema.hpp>
#include <iostream>

using jsoncons::json;
namespace jsonschema = jsoncons::jsonschema;

int main()
{
    json schema = json::parse(R"(
{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://test.json-schema.org/typical-dynamic-resolution/root",
    "$ref": "list",
    "$defs": {
        "foo": {
            "$dynamicAnchor": "items",
            "type": "string"
        },
        "list": {
            "$id": "list",
            "type": "array",
            "items": { "$dynamicRef": "#items" },
            "$defs": {
              "items": {
                  "$comment": "This is only needed to satisfy the bookending requirement",
                  "$dynamicAnchor": "items"
              }
            }
        }
    }
}
)");

    jsonschema::json_schema<json> compiled = jsonschema::make_json_schema(schema);

    json data = json::parse(R"(["foo", 42])");

    jsoncons::json_decoder<ojson> decoder;
    compiled.validate(data, decoder);
    ojson output = decoder.get_result();
    std::cout << pretty_print(output) << "\n\n";
}
```

Output:
```json
[
    {
        "valid": false,
        "evaluationPath": "/$ref/items/$dynamicRef/type",
        "schemaLocation": "https://test.json-schema.org/typical-dynamic-resolution/root#items",
        "instanceLocation": "/1",
        "error": "Expected string, found integer"
    }
]
```

#### Draft 2019-09 example (from the JSON Schema Test Suite)

```cpp
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonschema/jsonschema.hpp>
#include <iostream>

using jsoncons::json;
namespace jsonschema = jsoncons::jsonschema;

int main()
{
    json schema = json::parse(R"(
{
    "$schema": "https://json-schema.org/draft/2019-09/schema",
    "type": "object",
    "properties": {
        "foo": { "type": "string" }
    },
    "allOf": [
        {
            "properties": {
                "bar": { "type": "string" }
            }
        }
    ],
    "unevaluatedProperties": false
}
)");

    jsonschema::json_schema<json> compiled = jsonschema::make_json_schema(schema);

    json data = json::parse(R"({"foo": "foo","bar": "bar","baz": "baz"})");

    jsoncons::json_decoder<ojson> decoder;
    compiled.validate(data, decoder);
    ojson output = decoder.get_result();
    std::cout << pretty_print(output) << "\n\n";
}
```

Output:
```json
[
    {
        "valid": false,
        "evaluationPath": "/unevaluatedProperties/baz",
        "schemaLocation": "#",
        "instanceLocation": "/baz",
        "error": "Unevaluated property 'baz' but the schema does not allow unevaluated properties."
    }
]
```

#### Draft 07 example (from the JSON Schema Test Suite)

```cpp
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonschema/jsonschema.hpp>
#include <iostream>

using jsoncons::json;
namespace jsonschema = jsoncons::jsonschema;

int main()
{
    json schema = json::parse(R"(
{
    "items": [{}],
    "additionalItems": {"type": "integer"}
}
)");

    // Need to supply default version because schema does not have $schema keyword  
    jsonschema::json_schema<json> compiled = jsonschema::make_json_schema(schema,
        jsonschema::evaluation_options{}.default_version(jsonschema::schema_version::draft7()));

    json data = json::parse(R"([ null, 2, 3, "foo" ])");

    jsoncons::json_decoder<ojson> decoder;
    compiled.validate(data, decoder);
    ojson output = decoder.get_result();
    std::cout << pretty_print(output) << "\n\n";
}
```
Output:
```json
[
    {
        "valid": false,
        "evaluationPath": "/items/type",
        "schemaLocation": "#/additionalItems",
        "instanceLocation": "/3",
        "error": "Expected integer, found string"
    }
]
```

#### Cross draft example

```cpp
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonschema/jsonschema.hpp>
#include <iostream>

using jsoncons::json;
namespace jsonschema = jsoncons::jsonschema;

int main()
{
    json schema = json::parse(R"(
{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://example.com/schema",
    "$defs": {
        "foo": {
            "$schema": "http://json-schema.org/draft-07/schema#",
            "$id": "schema/foo",
            "definitions" : {
                "bar" : {
                    "type" : "string"
                }               
            }
        }       
    },
    "properties" : {
        "thing" : {
            "$ref" : "schema/foo#/definitions/bar"
        }
    }
}
)");
    jsonschema::json_schema<json> compiled = jsonschema::make_json_schema(schema);

    json data = json::parse(R"({"thing" : 10})");

    jsoncons::json_decoder<ojson> decoder;
    compiled.validate(data, decoder);
    ojson output = decoder.get_result();
    std::cout << pretty_print(output) << "\n\n";
}
```
Output:
```json
[
    {
        "valid": false,
        "evaluationPath": "/properties/thing/$ref/type",
        "schemaLocation": "https://example.com/schema/foo#/definitions/bar",
        "instanceLocation": "/thing",
        "error": "Expected string, found integer"
    }
]
```