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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
|
### jsonschema extension
Since 0.174.0, the jsonschema extension implements Drafts 4, 6, 7, 2019-9 and 2020-12 of the [JSON Schema Specification](https://json-schema.org/specification).
Previous versions supported Draft 7.
The documentation below describes the new features for the jsonschema extension since 0.174.0.
For earlier releases, please refer to [jsonschema (until 0.174.0)](https://github.com/danielaparker/jsoncons/tree/main).
### Classes
<table border="0">
<tr>
<td><a href="json_schema.md">json_schema</a></td>
<td>A <code>json_schema</code> represents the compiled form of a JSON Schema document.</td>
</tr>
<tr>
<td><a href="evaluation_options.md">evaluation_options</a></td>
<td>Allows configuration of JSON Schema evaluation.</td>
</tr>
<tr>
<td><a href="validation_message.md">validation_message</a></td>
<td>A message type for reporting errors generated by a keyword.</td>
</tr>
<tr>
<td><a href="schema_version.md">schema_version</a></td>
<td>Supported JSON Schema dialects.</td>
</tr>
<tr>
<td><a href="json_validator.md">json_validator</a></td>
<td>JSON Schema validator. Deprecated (since 0.174.0)</td>
</tr>
</table>
### Functions
<table border="0">
<tr>
<td><a href="make_json_schema.md">make_json_schema</a></td>
<td>Processes a JSON Schema document and returns the compiled form as a <code>json_schema</code> (since 0.174.0).
</tr>
<tr>
<td><a href="make_schema.md">make_schema</a></td>
<td>Loads a JSON Schema and returns a shared pointer to a <code>json_schema</code>. Deprecated since 0.174.0. Removed in 1.0.0.
</tr>
</table>
### Compliance
#### Keywords
The jsoncons implementation passes all required tests in the [JSON Schema Test Suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite/) for the keywords below.
You can check the [Bowtie project](https://github.com/bowtie-json-schema/bowtie) to see how jsoncons [compares with other implementations](https://bowtie.report/).
| Keyword | Draft 4 | Draft 6 | Draft 7 | Draft 2019-09 | Draft 2020-12 |
|:--------------------------:|:---------:|:---------:|:---------:|:---------:|:---------:|
| $anchor | | | | 🟢 | 🟢 |
| $defs | | | | 🟢 | 🟢 |
| $dynamicAnchor | | | | | 🟢 |
| $dynamicRef | | | | | 🟢 |
| $id | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| $recursiveAnchor | | | | 🟢 | |
| $recursiveRef | | | | 🟢 | |
| $ref | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| $vocabulary | | | | 🟢 | 🟢 |
| additionalItems | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| additionalProperties | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| allOf | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| anyOf | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| const | | 🟢 | 🟢 | 🟢 | 🟢 |
| contains | | 🟢 | 🟢 | 🟢 | 🟢 |
| contentEncoding | | | 🟢 | 🟢 | 🟢 |
| contentMediaType | | | 🟢 | 🟢 | 🟢 |
| definitions | 🟢 | 🟢 | 🟢 | | |
| dependencies | 🟢 | 🟢 | 🟢 | | |
| dependentRequired | | | | 🟢 | 🟢 |
| dependentSchemas | | | | 🟢 | 🟢 |
| enum | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| exclusiveMaximum (boolean) | 🟢 | | | | |
| exclusiveMaximum | | 🟢 | 🟢 | 🟢 | 🟢 |
| exclusiveMinimum (boolean) | 🟢 | | | | |
| exclusiveMinimum | | 🟢 | 🟢 | 🟢 | 🟢 |
| if-then-else | | | 🟢 | 🟢 | 🟢 |
| items | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| maxContains | | | | 🟢 | 🟢 |
| minContains | | | | 🟢 | 🟢 |
| maximum | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| maxItems | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| maxLength | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| maxProperties | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| minimum | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| minItems | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| minLength | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| minProperties | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| multipleOf | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| not | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| oneOf | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| pattern | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| patternProperties | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| prefixItems | | | | | 🟢 |
| properties | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| propertyNames | | 🟢 | 🟢 | 🟢 | 🟢 |
| required | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| type | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| unevaluatedItems | | | | 🟢 | 🟢 |
| unevaluatedProperties | | | | 🟢 | 🟢 |
| uniqueItems | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
#### Format
The implementation understands the following [formats](https://json-schema.org/understanding-json-schema/reference/string.html#format):
| Format | Draft 4 | Draft 6 | Draft 7 | Draft 2019-09 | Draft 2020-12 |
|:-------------:|:---------:|:---------:|:---------:|:---------:|:---------:|
| date | | 🟢 | 🟢 | 🟢 | 🟢 |
| date-time | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| email | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| hostname | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| ipv4 | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| ipv6 | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| json-pointer | | 🟢 | 🟢 | 🟢 | 🟢 |
| regex | 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| time | | 🟢 | 🟢 | 🟢 | 🟢 |
| uri (since 1.0.0)| 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
| uri-reference (since 1.0.0)| 🟢 | 🟢 | 🟢 | 🟢 | 🟢 |
Any other format type is ignored.
Since Draft 2019-09, format is no longer an assertion by default. It can be configured to be an assertion
by setting the evaluation option `require_format_validation` to `true`
### Schema dialect
jsoncons supports Drafts 4, 6, 7, 2019-9 and 2020-12 of the [JSON Schema Specification](https://json-schema.org/specification).
It needs to know the dialect of a schema resource in order to know how to process it. If a schema contains a **$schema** dialect identifier,
it uses that. Otherwise, it falls back on a default version you can provide through [evaluation_options](./evaluation_options.md), e.g.
```cpp
auto options = jsonschema::evaluation_options{}
.default_version(jsonschema::schema_version::draft7());
auto compiled = jsonschema::make_json_schema(schema, options);
```
If no default is provided, the latest supported version is assumed.
### URI resolution
jsoncons requires a user supplied schema resolver to resolve a URI reference to a schema resoure.
A schema resolver is a function object with the signature of `resolver` 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>.
Here is an example of a schema resolver that resolves a URI to a physical pathname,
```cpp
std::string root_dir = "./input/jsonschema";
auto resolver = [&](const jsoncons::uri& uri) -> json
{
std::string pathname = root_dir + uri.path();
std::fstream is(pathname.c_str());
if (!is)
{
return json::null();
}
return json::parse(is);
};
```
### Default values
The JSON Schema Specification includes the ["default" keyword](https://json-schema.org/understanding-json-schema/reference/generic.html)
for specifying a default value, but doesn't prescribe how implementations should use it during validation.
Some implementations ignore the default keyword, others support updating the input JSON to fill in a default value
for a missing key/value pair. This implementation outputs a JSONPatch document that may be further applied to the input JSON to add the
missing key/value pairs.
### Custom error messages (since 1.2.0)
jsoncons supports custom error messages via an opt-in `errorMessage` keyword.
To enable this feature, refer to [custom error messages](./custom-error-messages.md).
### Examples
The example schemas are from [JSON Schema Miscellaneous Examples](https://json-schema.org/learn/miscellaneous-examples.html),
the [JSON Schema Test Suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite), and user contributions.
[Three ways of validating](#eg1)
[Format validation](#eg2)
[Resolving references to schemas defined in external files](#eg3)
[Validate before decoding JSON into C++ class objects](#eg4)
[Default values](#eg5)
<div id="eg1"/>
#### Three ways of validating
This example illustrates the use of three overloads of the `validate` function that throw,
invoke a callback function, and write to a `json_visitor`.
```cpp
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonschema/jsonschema.hpp>
#include <iostream>
using jsoncons::ojson;
namespace jsonschema = jsoncons::jsonschema;
int main()
std::string schema_str = R"(
{
"$id": "https://example.com/arrays.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "A representation of a person, company, organization, or place",
"type": "object",
"properties": {
"fruits": {
"type": "array",
"items": {
"type": "string"
}
},
"vegetables": {
"type": "array",
"items": { "$ref": "#/$defs/veggie" }
}
},
"$defs": {
"veggie": {
"type": "object",
"required": [ "veggieName", "veggieLike" ],
"properties": {
"veggieName": {
"type": "string",
"description": "The name of the vegetable."
},
"veggieLike": {
"type": "boolean",
"description": "Do I like this vegetable?"
}
}
}
}
}
)";
std::string data_str = R"(
{
"fruits": [ "apple", "orange", "pear" ],
"vegetables": [
{
"veggieName": "potato",
"veggieLike": true
},
{
"veggieName": "broccoli",
"veggieLike": "false"
},
{
"veggieName": "carrot",
"veggieLike": false
},
{
"veggieName": "Swiss Chard"
}
]
}
)";
ojson schema = ojson::parse(schema_str);
jsonschema::json_schema<ojson> compiled = jsonschema::make_json_schema(std::move(schema));
ojson data = ojson::parse(data_str);
std::cout << "\n(1) Validate using exceptions\n";
try
{
compiled.validate(data);
}
catch (const std::exception& e)
{
std::cout << e.what() << "\n";
}
std::cout << "\n(2) Validate using reporter callback\n";
auto reporter = [](const jsonschema::validation_message& message) -> jsonschema::walk_result
{
std::cout << message.instance_location().string() << ": " << message.message() << "\n";
return jsonschema::walk_result::advance;
};
compiled.validate(data, reporter);
std::cout << "\n(3) Validate outputting to a json decoder\n";
jsoncons::json_decoder<ojson> decoder;
compiled.validate(data, decoder);
ojson output = decoder.get_result();
std::cout << pretty_print(output) << "\n";
}
```
Output:
```
(1) Validate using exceptions
/vegetables/1/veggieLike: Expected boolean, found string
(2) Validate using reporter callback
/vegetables/1/veggieLike: Expected boolean, found string
/vegetables/3: Required property 'veggieLike' not found.
(3) Validate outputting to a json decoder
[
{
"valid": false,
"evaluationPath": "/properties/vegetables/items/$ref/properties/veggieLike/type",
"schemaLocation": "https://example.com/arrays.schema.json#/$defs/veggie/properties/veggieLike",
"instanceLocation": "/vegetables/1/veggieLike",
"error": "Expected boolean, found string"
},
{
"valid": false,
"evaluationPath": "/properties/vegetables/items/$ref/required",
"schemaLocation": "https://example.com/arrays.schema.json#/$defs/veggie/required",
"instanceLocation": "/vegetables/3",
"error": "Required property 'veggieLike' not found."
}
]
```
<div id="eg2"/>
#### Format validation
Since Draft 2019-09, format validation is disabled by default, but may be enabled by setting the
evaluation option `require_format_validation` to `true`.
```cpp
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonschema/jsonschema.hpp>
#include <iostream>
using jsoncons::json;
using jsoncons::ojson;
namespace jsonschema = jsoncons::jsonschema;
int main()
{
json schema = json::parse(R"(
{
"$id": "/test_schema",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"Date": {
"format": "date-time",
"type": "string"
}
},
"required": [
"Date"
],
"type": "object",
"unevaluatedProperties": false
}
)");
auto compiled = jsoncons::jsonschema::make_json_schema(schema,
jsonschema::evaluation_options{}.require_format_validation(true));
json data = json::parse(R"(
{ "Date" : "2024-03-19T26:34:56Z" }
)");
jsoncons::json_decoder<ojson> decoder;
compiled.validate(data, decoder);
ojson output = decoder.get_result();
std::cout << pretty_print(output) << "\n";
}
```
Output:
```json
[
{
"valid": false,
"evaluationPath": "/properties/Date/format",
"schemaLocation": "/test_schema#/properties/Date/format",
"instanceLocation": "/Date",
"error": "'2024-03-19T26:34:56Z' is not a RFC 3339 date-time string."
},
{
"valid": false,
"evaluationPath": "/unevaluatedProperties/Date",
"schemaLocation": "/test_schema",
"instanceLocation": "/Date",
"error": "Unevaluated property 'Date' but the schema does not allow unevaluated properties."
}
]
```
<div id="eg3"/>
#### Resolving references to schemas defined in external files
In this example, the main schema is defined by
```cpp
std::string main_schema = R"(
{
"$id" : "https://www.example.com/main",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "http://localhost:1234/draft2020-12/object",
"type": "object",
"properties": {
"name": {"$ref": "/name-defs.json#/$defs/orNull"}
}
}
)";
```
The main schema includes a reference using the `$ref` keyword to a
second schema defined in an external file, `name-defs.json`,
```json
{
"$id" : "https://www.example.com/other",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$defs": {
"orNull": {
"anyOf": [
{
"type": "null"
},
{
"$ref": "#"
}
]
}
},
"type": "string"
}
```
When building the main schema, the schema builder needs to resolve the URI 'https://www.example.com/name-defs.json#/$defs/orNull'
via a URI resolver. The resolver does not need to supply that specific subschema, it is enough to supply the schema document '/name-defs.json'.
The schema builder than processes that schema document and makes multiple entries into an internal validator registry,
including an entry for 'https://www.example.com/name-defs.json#/$defs/orNull'.
```cpp
int main()
{
json schema = json::parse(main_schema);
// Data
json data = json::parse(R"(
{
"name": {
"name": null
}
}
)");
try
{
// Throws schema_error if JSON Schema compilation fails
jsonschema::json_schema<json> compiled = jsonschema::make_json_schema(schema, resolver);
std::string root_dir = "./input/jsonschema";
auto resolver = [&](const jsoncons::uri& uri) -> json
{
std::cout << "Requested URI: " << uri.string() << "\n";
std::cout << "base: " << uri.base().string() << ", path: " << uri.path() << "\n\n";
std::string pathname = root_dir + uri.path();
std::fstream is(pathname.c_str());
if (!is)
{
return json::null();
}
return json::parse(is);
};
// Will call the message handler function object for each schema violation
compiled.validate(data, reporter);
}
catch (const std::exception& e)
{
std::cout << e.what() << '\n';
}
}
```
Output:
```
Requested URI: https://www.example.com/name-defs.json#/$defs/orNull
base: https://www.example.com/name-defs.json, path: /name-defs.json
/name: Must be valid against at least one schema, but found no matching schemas
Expected null, found object
Expected string, found object
```
<div id="eg4"/>
#### Validate before decoding JSON into C++ class objects
This example illustrates decoding data that validates against "oneOf"
into a `std::variant`.
```cpp
// for brevity
using jsoncons::json;
namespace jsonschema = jsoncons::jsonschema;
int main()
{
std::string schema_str = R"(
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "job",
"description": "job properties json schema",
"$defs": {
"os_properties": {
"type": "object",
"properties": {
"command": {
"description": "this is the OS command to run",
"type": "string",
"minLength": 1
}
},
"required": [ "command" ],
"additionalProperties": false
},
"db_properties": {
"type": "object",
"properties": {
"query": {
"description": "this is db query to run",
"type": "string",
"minLength": 1
}
},
"required": [ "query" ],
"additionalProperties": false
},
"api_properties": {
"type": "object",
"properties": {
"target": {
"description": "this is api target to run",
"type": "string",
"minLength": 1
}
},
"required": [ "target" ],
"additionalProperties": false
}
},
"type": "object",
"properties": {
"name": {
"description": "name of the flow",
"type": "string",
"minLength": 1
},
"run": {
"description": "job run properties",
"type": "object",
"oneOf": [
{ "$ref": "#/$defs/os_properties" },
{ "$ref": "#/$defs/db_properties" },
{ "$ref": "#/$defs/api_properties" }
]
}
},
"required": [ "name", "run" ],
"additionalProperties": false
}
)";
std::string data_str = R"(
{
"name": "testing flow",
"run" : {
"command": "some command"
}
}
)";
try
{
json schema = json::parse(schema_str);
json data = json::parse(data_str);
// Throws schema_error if JSON Schema compilation fails
jsonschema::json_schema<json> compiled = jsonschema::make_json_schema(schema);
// Test that input is valid before attempting to decode
if (compiled.is_valid(data))
{
const ns::job_properties v = data.as<ns::job_properties>(); // You don't need to reparse data_str
std::string output;
jsoncons::encode_json_pretty(v, output);
std::cout << output << std::endl;
// Verify that output is valid
json test = json::parse(output);
assert(compiled.is_valid(test));
}
else
{
std::cout << "Invalid input\n";
}
}
catch (const std::exception& e)
{
std::cout << e.what() << '\n';
}
}
```
Output:
```
{
"name": "testing flow",
"run": {
"command": "some command"
}
}
```
<div id="eg5"/>
#### Default values
```cpp
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonschema/jsonschema.hpp>
#include <jsoncons_ext/jsonpatch/jsonpatch.hpp>
#include <fstream>
// for brevity
using jsoncons::json;
namespace jsonschema = jsoncons::jsonschema;
namespace jsonpatch = jsoncons::jsonpatch;
int main()
{
json schema = json::parse(R"(
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"bar": {
"type": "string",
"minLength": 4,
"default": "bad"
}
}
}
)");
try
{
// Data
json data = json::parse("{}");
// will throw schema_error if JSON Schema compilation fails
jsonschema::json_schema<json> compiled = jsonschema::make_json_schema(schema);
// will throw a validation_error when a schema violation happens
json patch;
compiled.validate(data, patch);
std::cout << "Patch: " << patch << "\n";
std::cout << "Original data: " << data << "\n";
jsonpatch::apply_patch(data, patch);
std::cout << "Patched data: " << data << "\n\n";
}
catch (const std::exception& e)
{
std::cout << e.what() << "\n";
}
}
```
Output:
```
Patch: [{"op":"add","path":"/bar","value":"bad"}]
Original data: {}
Patched data: {"bar":"bad"}
```
|