Debian Codegraph @

LspCpp/third_party/rapidjson/doc/schema.md

raw
1 # Schema
2
3 (This feature was released in v1.1.0)
4
5 JSON Schema is a draft standard for describing the format of JSON data. The schema itself is also JSON data. By validating a JSON structure with JSON Schema, your code can safely access the DOM without manually checking types, or whether a key exists, etc. It can also ensure that the serialized JSON conform to a specified schema.
6
7 RapidJSON implemented a JSON Schema validator for [JSON Schema Draft v4](http://json-schema.org/documentation.html). If you are not familiar with JSON Schema, you may refer to [Understanding JSON Schema](http://spacetelescope.github.io/understanding-json-schema/).
8
9 [TOC]
10
11 # Basic Usage {#Basic}
12
13 First of all, you need to parse a JSON Schema into `Document`, and then compile the `Document` into a `SchemaDocument`.
14
15 Secondly, construct a `SchemaValidator` with the `SchemaDocument`. It is similar to a `Writer` in the sense of handling SAX events. So, you can use `document.Accept(validator)` to validate a document, and then check the validity.
16
17 ~~~cpp
18 #include "rapidjson/schema.h"
19
20 // ...
21
22 Document sd;
23 if (sd.Parse(schemaJson).HasParseError()) {
24 // the schema is not a valid JSON.
25 // ...
26 }
27
28 SchemaDocument schema(sd); // Compile a Document to SchemaDocument
29 if (!schema.GetError().ObjectEmpty()) {
30 // there was a problem compiling the schema
31 StringBuffer sb;
32 Writer<StringBuffer> w(sb);
33 schema.GetError().Accept(w);
34 printf("Invalid schema: %s\n", sb.GetString());
35 }
36 // sd is no longer needed here.
37
38 Document d;
39 if (d.Parse(inputJson).HasParseError()) {
40 // the input is not a valid JSON.
41 // ...
42 }
43
44 SchemaValidator validator(schema);
45 if (!d.Accept(validator)) {
46 // Input JSON is invalid according to the schema
47 // Output diagnostic information
48 StringBuffer sb;
49 validator.GetInvalidSchemaPointer().StringifyUriFragment(sb);
50 printf("Invalid schema: %s\n", sb.GetString());
51 printf("Invalid keyword: %s\n", validator.GetInvalidSchemaKeyword());
52 sb.Clear();
53 validator.GetInvalidDocumentPointer().StringifyUriFragment(sb);
54 printf("Invalid document: %s\n", sb.GetString());
55 }
56 ~~~
57
58 Some notes:
59
60 * One `SchemaDocument` can be referenced by multiple `SchemaValidator`s. It will not be modified by `SchemaValidator`s.
61 * A `SchemaValidator` may be reused to validate multiple documents. To run it for other documents, call `validator.Reset()` first.
62
63 # Validation during parsing/serialization {#Fused}
64
65 Unlike most JSON Schema validator implementations, RapidJSON provides a SAX-based schema validator. Therefore, you can parse a JSON from a stream while validating it on the fly. If the validator encounters a JSON value that invalidates the supplied schema, the parsing will be terminated immediately. This design is especially useful for parsing large JSON files.
66
67 ## DOM parsing {#DOM}
68
69 For using DOM in parsing, `Document` needs some preparation and finalizing tasks, in addition to receiving SAX events, thus it needs some work to route the reader, validator and the document. `SchemaValidatingReader` is a helper class that doing such work.
70
71 ~~~cpp
72 #include "rapidjson/filereadstream.h"
73
74 // ...
75 SchemaDocument schema(sd); // Compile a Document to SchemaDocument
76
77 // Use reader to parse the JSON
78 FILE* fp = fopen("big.json", "r");
79 FileReadStream is(fp, buffer, sizeof(buffer));
80
81 // Parse JSON from reader, validate the SAX events, and store in d.
82 Document d;
83 SchemaValidatingReader<kParseDefaultFlags, FileReadStream, UTF8<> > reader(is, schema);
84 d.Populate(reader);
85
86 if (!reader.GetParseResult()) {
87 // Not a valid JSON
88 // When reader.GetParseResult().Code() == kParseErrorTermination,
89 // it may be terminated by:
90 // (1) the validator found that the JSON is invalid according to schema; or
91 // (2) the input stream has I/O error.
92
93 // Check the validation result
94 if (!reader.IsValid()) {
95 // Input JSON is invalid according to the schema
96 // Output diagnostic information
97 StringBuffer sb;
98 reader.GetInvalidSchemaPointer().StringifyUriFragment(sb);
99 printf("Invalid schema: %s\n", sb.GetString());
100 printf("Invalid keyword: %s\n", reader.GetInvalidSchemaKeyword());
101 sb.Clear();
102 reader.GetInvalidDocumentPointer().StringifyUriFragment(sb);
103 printf("Invalid document: %s\n", sb.GetString());
104 }
105 }
106 ~~~
107
108 ## SAX parsing {#SAX}
109
110 For using SAX in parsing, it is much simpler. If it only need to validate the JSON without further processing, it is simply:
111
112 ~~~
113 SchemaValidator validator(schema);
114 Reader reader;
115 if (!reader.Parse(stream, validator)) {
116 if (!validator.IsValid()) {
117 // ...
118 }
119 }
120 ~~~
121
122 This is exactly the method used in the [schemavalidator](example/schemavalidator/schemavalidator.cpp) example. The distinct advantage is low memory usage, no matter how big the JSON was (the memory usage depends on the complexity of the schema).
123
124 If you need to handle the SAX events further, then you need to use the template class `GenericSchemaValidator` to set the output handler of the validator:
125
126 ~~~
127 MyHandler handler;
128 GenericSchemaValidator<SchemaDocument, MyHandler> validator(schema, handler);
129 Reader reader;
130 if (!reader.Parse(ss, validator)) {
131 if (!validator.IsValid()) {
132 // ...
133 }
134 }
135 ~~~
136
137 ## Serialization {#Serialization}
138
139 It is also possible to do validation during serializing. This can ensure the result JSON is valid according to the JSON schema.
140
141 ~~~
142 StringBuffer sb;
143 Writer<StringBuffer> writer(sb);
144 GenericSchemaValidator<SchemaDocument, Writer<StringBuffer> > validator(s, writer);
145 if (!d.Accept(validator)) {
146 // Some problem during Accept(), it may be validation or encoding issues.
147 if (!validator.IsValid()) {
148 // ...
149 }
150 }
151 ~~~
152
153 Of course, if your application only needs SAX-style serialization, it can simply send SAX events to `SchemaValidator` instead of `Writer`.
154
155 # Remote Schema {#Remote}
156
157 JSON Schema supports [`$ref` keyword](http://spacetelescope.github.io/understanding-json-schema/structuring.html), which is a [JSON pointer](doc/pointer.md) referencing to a local or remote schema. Local pointer is prefixed with `#`, while remote pointer is an relative or absolute URI. For example:
158
159 ~~~js
160 { "$ref": "definitions.json#/address" }
161 ~~~
162
163 As `SchemaDocument` does not know how to resolve such URI, it needs a user-provided `IRemoteSchemaDocumentProvider` instance to do so.
164
165 ~~~
166 class MyRemoteSchemaDocumentProvider : public IRemoteSchemaDocumentProvider {
167 public:
168 virtual const SchemaDocument* GetRemoteDocument(const char* uri, SizeType length) {
169 // Resolve the uri and returns a pointer to that schema.
170 }
171 };
172
173 // ...
174
175 MyRemoteSchemaDocumentProvider provider;
176 SchemaDocument schema(sd, &provider);
177 ~~~
178
179 # Conformance {#Conformance}
180
181 RapidJSON passed 262 out of 263 tests in [JSON Schema Test Suite](https://github.com/json-schema/JSON-Schema-Test-Suite) (Json Schema draft 4).
182
183 The failed test is "changed scope ref invalid" of "change resolution scope" in `refRemote.json`. It is due to that `id` schema keyword and URI combining function are not implemented.
184
185 Besides, the `format` schema keyword for string values is ignored, since it is not required by the specification.
186
187 ## Regular Expression {#Regex}
188
189 The schema keyword `pattern` and `patternProperties` uses regular expression to match the required pattern.
190
191 RapidJSON implemented a simple NFA regular expression engine, which is used by default. It supports the following syntax.
192
193 |Syntax|Description|
194 |------|-----------|
195 |`ab` | Concatenation |
196 |<code>a&#124;b</code> | Alternation |
197 |`a?` | Zero or one |
198 |`a*` | Zero or more |
199 |`a+` | One or more |
200 |`a{3}` | Exactly 3 times |
201 |`a{3,}` | At least 3 times |
202 |`a{3,5}`| 3 to 5 times |
203 |`(ab)` | Grouping |
204 |`^a` | At the beginning |
205 |`a$` | At the end |
206 |`.` | Any character |
207 |`[abc]` | Character classes |
208 |`[a-c]` | Character class range |
209 |`[a-z0-9_]` | Character class combination |
210 |`[^abc]` | Negated character classes |
211 |`[^a-c]` | Negated character class range |
212 |`[\b]` | Backspace (U+0008) |
213 |<code>\\&#124;</code>, `\\`, ... | Escape characters |
214 |`\f` | Form feed (U+000C) |
215 |`\n` | Line feed (U+000A) |
216 |`\r` | Carriage return (U+000D) |
217 |`\t` | Tab (U+0009) |
218 |`\v` | Vertical tab (U+000B) |
219
220 For C++11 compiler, it is also possible to use the `std::regex` by defining `RAPIDJSON_SCHEMA_USE_INTERNALREGEX=0` and `RAPIDJSON_SCHEMA_USE_STDREGEX=1`. If your schemas do not need `pattern` and `patternProperties`, you can set both macros to zero to disable this feature, which will reduce some code size.
221
222 # Performance {#Performance}
223
224 Most C++ JSON libraries do not yet support JSON Schema. So we tried to evaluate the performance of RapidJSON's JSON Schema validator according to [json-schema-benchmark](https://github.com/ebdrup/json-schema-benchmark), which tests 11 JavaScript libraries running on Node.js.
225
226 That benchmark runs validations on [JSON Schema Test Suite](https://github.com/json-schema/JSON-Schema-Test-Suite), in which some test suites and tests are excluded. We made the same benchmarking procedure in [`schematest.cpp`](test/perftest/schematest.cpp).
227
228 On a Mac Book Pro (2.8 GHz Intel Core i7), the following results are collected.
229
230 |Validator|Relative speed|Number of test runs per second|
231 |---------|:------------:|:----------------------------:|
232 |RapidJSON|155%|30682|
233 |[`ajv`](https://github.com/epoberezkin/ajv)|100%|19770 (± 1.31%)|
234 |[`is-my-json-valid`](https://github.com/mafintosh/is-my-json-valid)|70%|13835 (± 2.84%)|
235 |[`jsen`](https://github.com/bugventure/jsen)|57.7%|11411 (± 1.27%)|
236 |[`schemasaurus`](https://github.com/AlexeyGrishin/schemasaurus)|26%|5145 (± 1.62%)|
237 |[`themis`](https://github.com/playlyfe/themis)|19.9%|3935 (± 2.69%)|
238 |[`z-schema`](https://github.com/zaggino/z-schema)|7%|1388 (± 0.84%)|
239 |[`jsck`](https://github.com/pandastrike/jsck#readme)|3.1%|606 (± 2.84%)|
240 |[`jsonschema`](https://github.com/tdegrunt/jsonschema#readme)|0.9%|185 (± 1.01%)|
241 |[`skeemas`](https://github.com/Prestaul/skeemas#readme)|0.8%|154 (± 0.79%)|
242 |tv4|0.5%|93 (± 0.94%)|
243 |[`jayschema`](https://github.com/natesilva/jayschema)|0.1%|21 (± 1.14%)|
244
245 That is, RapidJSON is about 1.5x faster than the fastest JavaScript library (ajv). And 1400x faster than the slowest one.
246
247 # Schema violation reporting {#Reporting}
248
249 (Unreleased as of 2017-09-20)
250
251 When validating an instance against a JSON Schema,
252 it is often desirable to report not only whether the instance is valid,
253 but also the ways in which it violates the schema.
254
255 The `SchemaValidator` class
256 collects errors encountered during validation
257 into a JSON `Value`.
258 This error object can then be accessed as `validator.GetError()`.
259
260 The structure of the error object is subject to change
261 in future versions of RapidJSON,
262 as there is no standard schema for violations.
263 The details below this point are provisional only.
264
265 ## General provisions {#ReportingGeneral}
266
267 Validation of an instance value against a schema
268 produces an error value.
269 The error value is always an object.
270 An empty object `{}` indicates the instance is valid.
271
272 * The name of each member
273 corresponds to the JSON Schema keyword that is violated.
274 * The value is either an object describing a single violation,
275 or an array of such objects.
276
277 Each violation object contains two string-valued members
278 named `instanceRef` and `schemaRef`.
279 `instanceRef` contains the URI fragment serialization
280 of a JSON Pointer to the instance subobject
281 in which the violation was detected.
282 `schemaRef` contains the URI of the schema
283 and the fragment serialization of a JSON Pointer
284 to the subschema that was violated.
285
286 Individual violation objects can contain other keyword-specific members.
287 These are detailed further.
288
289 For example, validating this instance:
290
291 ~~~json
292 {"numbers": [1, 2, "3", 4, 5]}
293 ~~~
294
295 against this schema:
296
297 ~~~json
298 {
299 "type": "object",
300 "properties": {
301 "numbers": {"$ref": "numbers.schema.json"}
302 }
303 }
304 ~~~
305
306 where `numbers.schema.json` refers
307 (via a suitable `IRemoteSchemaDocumentProvider`)
308 to this schema:
309
310 ~~~json
311 {
312 "type": "array",
313 "items": {"type": "number"}
314 }
315 ~~~
316
317 produces the following error object:
318
319 ~~~json
320 {
321 "type": {
322 "instanceRef": "#/numbers/2",
323 "schemaRef": "numbers.schema.json#/items",
324 "expected": ["number"],
325 "actual": "string"
326 }
327 }
328 ~~~
329
330 ## Validation keywords for numbers {#Numbers}
331
332 ### multipleOf {#multipleof}
333
334 * `expected`: required number strictly greater than 0.
335 The value of the `multipleOf` keyword specified in the schema.
336 * `actual`: required number.
337 The instance value.
338
339 ### maximum {#maximum}
340
341 * `expected`: required number.
342 The value of the `maximum` keyword specified in the schema.
343 * `exclusiveMaximum`: optional boolean.
344 This will be true if the schema specified `"exclusiveMaximum": true`,
345 and will be omitted otherwise.
346 * `actual`: required number.
347 The instance value.
348
349 ### minimum {#minimum}
350
351 * `expected`: required number.
352 The value of the `minimum` keyword specified in the schema.
353 * `exclusiveMinimum`: optional boolean.
354 This will be true if the schema specified `"exclusiveMinimum": true`,
355 and will be omitted otherwise.
356 * `actual`: required number.
357 The instance value.
358
359 ## Validation keywords for strings {#Strings}
360
361 ### maxLength {#maxLength}
362
363 * `expected`: required number greater than or equal to 0.
364 The value of the `maxLength` keyword specified in the schema.
365 * `actual`: required string.
366 The instance value.
367
368 ### minLength {#minLength}
369
370 * `expected`: required number greater than or equal to 0.
371 The value of the `minLength` keyword specified in the schema.
372 * `actual`: required string.
373 The instance value.
374
375 ### pattern {#pattern}
376
377 * `actual`: required string.
378 The instance value.
379
380 (The expected pattern is not reported
381 because the internal representation in `SchemaDocument`
382 does not store the pattern in original string form.)
383
384 ## Validation keywords for arrays {#Arrays}
385
386 ### additionalItems {#additionalItems}
387
388 This keyword is reported
389 when the value of `items` schema keyword is an array,
390 the value of `additionalItems` is `false`,
391 and the instance is an array
392 with more items than specified in the `items` array.
393
394 * `disallowed`: required integer greater than or equal to 0.
395 The index of the first item that has no corresponding schema.
396
397 ### maxItems and minItems {#maxItems-minItems}
398
399 * `expected`: required integer greater than or equal to 0.
400 The value of `maxItems` (respectively, `minItems`)
401 specified in the schema.
402 * `actual`: required integer greater than or equal to 0.
403 Number of items in the instance array.
404
405 ### uniqueItems {#uniqueItems}
406
407 * `duplicates`: required array
408 whose items are integers greater than or equal to 0.
409 Indices of items of the instance that are equal.
410
411 (RapidJSON only reports the first two equal items,
412 for performance reasons.)
413
414 ## Validation keywords for objects
415
416 ### maxProperties and minProperties {#maxProperties-minProperties}
417
418 * `expected`: required integer greater than or equal to 0.
419 The value of `maxProperties` (respectively, `minProperties`)
420 specified in the schema.
421 * `actual`: required integer greater than or equal to 0.
422 Number of properties in the instance object.
423
424 ### required {#required}
425
426 * `missing`: required array of one or more unique strings.
427 The names of properties
428 that are listed in the value of the `required` schema keyword
429 but not present in the instance object.
430
431 ### additionalProperties {#additionalProperties}
432
433 This keyword is reported
434 when the schema specifies `additionalProperties: false`
435 and the name of a property of the instance is
436 neither listed in the `properties` keyword
437 nor matches any regular expression in the `patternProperties` keyword.
438
439 * `disallowed`: required string.
440 Name of the offending property of the instance.
441
442 (For performance reasons,
443 RapidJSON only reports the first such property encountered.)
444
445 ### dependencies {#dependencies}
446
447 * `errors`: required object with one or more properties.
448 Names and values of its properties are described below.
449
450 Recall that JSON Schema Draft 04 supports
451 *schema dependencies*,
452 where presence of a named *controlling* property
453 requires the instance object to be valid against a subschema,
454 and *property dependencies*,
455 where presence of a controlling property
456 requires other *dependent* properties to be also present.
457
458 For a violated schema dependency,
459 `errors` will contain a property
460 with the name of the controlling property
461 and its value will be the error object
462 produced by validating the instance object
463 against the dependent schema.
464
465 For a violated property dependency,
466 `errors` will contain a property
467 with the name of the controlling property
468 and its value will be an array of one or more unique strings
469 listing the missing dependent properties.
470
471 ## Validation keywords for any instance type {#AnyTypes}
472
473 ### enum {#enum}
474
475 This keyword has no additional properties
476 beyond `instanceRef` and `schemaRef`.
477
478 * The allowed values are not listed
479 because `SchemaDocument` does not store them in original form.
480 * The violating value is not reported
481 because it might be unwieldy.
482
483 If you need to report these details to your users,
484 you can access the necessary information
485 by following `instanceRef` and `schemaRef`.
486
487 ### type {#type}
488
489 * `expected`: required array of one or more unique strings,
490 each of which is one of the seven primitive types
491 defined by the JSON Schema Draft 04 Core specification.
492 Lists the types allowed by the `type` schema keyword.
493 * `actual`: required string, also one of seven primitive types.
494 The primitive type of the instance.
495
496 ### allOf, anyOf, and oneOf {#allOf-anyOf-oneOf}
497
498 * `errors`: required array of at least one object.
499 There will be as many items as there are subschemas
500 in the `allOf`, `anyOf` or `oneOf` schema keyword, respectively.
501 Each item will be the error value
502 produced by validating the instance
503 against the corresponding subschema.
504
505 For `allOf`, at least one error value will be non-empty.
506 For `anyOf`, all error values will be non-empty.
507 For `oneOf`, either all error values will be non-empty,
508 or more than one will be empty.
509
510 ### not {#not}
511
512 This keyword has no additional properties
513 apart from `instanceRef` and `schemaRef`.