You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*[JSON report of the failures](#json-report-of-the-failures)
12
12
*[Eary failure mode](#early-failure-mode)
13
13
*[Default values](#default-values)
14
+
*[readOnly and writeOnly context](#readonly-and-writeonly-context)
14
15
*[Format validators](#format-validators)
15
16
*[Example](#example)
16
17
*[Resolution scopes](#resolution-scopes)
17
18
18
19
<ahref="http://jetbrains.com"><imgsrc="./jetbrains-logo.png" /></a> Supported by JetBrains.
19
20
20
-
This project is an implementation of the JSON Schema [Draft v4][draft-zyp-json-schema-04] and [Draft v6](https://tools.ietf.org/html/draft-wright-json-schema-01) specifications.
21
+
This project is an implementation of the JSON Schema [Draft v4][draft-zyp-json-schema-04], [Draft v6](https://tools.ietf.org/html/draft-wright-json-schema-01) and [Draft v7](https://tools.ietf.org/html/draft-handrews-json-schema-validation-00) specifications.
21
22
It uses the [org.json API](http://stleary.github.io/JSON-java/) (created by Douglas Crockford) for representing JSON data.
22
23
23
24
# When to use this library?
@@ -27,7 +28,7 @@ But - as you may have already discovered - there is also an [other Java implemen
27
28
of the JSON Schema specification. So here are some advices about which one to use:
28
29
* if you use Jackson to handle JSON in Java code, then [java-json-tools/json-schema-validator] is obviously a better choice, since it uses Jackson
29
30
* if you want to use the [org.json API](http://stleary.github.io/JSON-java/) then this library is the better choice
30
-
* if you need JSON Schema Draft 6 support, then you need this library.
31
+
* if you need JSON Schema Draft 6 / 7 support, then you need this library.
31
32
* if you want to use anything else for handling JSON (like GSON or javax.json), then you are in a little trouble, since
32
33
currently there is no schema validation library backed by these libraries. It means that you will have to parse the JSON
33
34
twice: once for the schema validator, and once for your own processing. In a case like that, this library is probably still
@@ -42,7 +43,7 @@ Add the JitPack repository and the dependency to your `pom.xml` as follows:
JSON Schema has currently 3 major releases, Draft 3, Draft 4and Draft 6. This library implements the 2 newer ones, you can have a quick look at the differences [here](https://github.com/json-schema-org/json-schema-spec/wiki/FAQ:-draft-wright-json-schema%5B-validation%5D-01#changes).
89
+
JSON Schema has currently 4 major releases, Draft 3, Draft 4, Draft 6 and Draft 7. This library implements the 3 newer ones, you can have a quick look at the differences [here](https://github.com/json-schema-org/json-schema-spec/wiki/FAQ:-draft-wright-json-schema%5B-validation%5D-01#changes) and [here](https://tools.ietf.org/html/draft-handrews-json-schema-validation-00#appendix-B).
89
90
Since the two versions have a number of differences - and draft 6 is not backwards-compatible with draft 4 - it is good to know which version will you use.
90
91
91
92
The best way to denote the JSON Schema version you want to use is to include its meta-schema URL in the document root with the `"$schema"` key. This is a common notation, facilitated by the library to determine which version should be used.
92
93
93
94
Quick reference:
94
95
* if there is `"$schema": "http://json-schema.org/draft-04/schema"` in the schema root, then Draft 4 will be used
95
96
* if there is `"$schema": "http://json-schema.org/draft-06/schema"` in the schema root, then Draft 6 will be used
97
+
* if there is `"$schema": "http://json-schema.org/draft-07/schema"` in the schema root, then Draft 7 will be used
96
98
* if none of these is found then Draft 4 will be assumed as default
97
99
98
-
If you want to specify the meta-schema version explicitly then you can change the default from Draft 4 to Draft 6 by configuring the loader this way:
100
+
If you want to specify the meta-schema version explicitly then you can change the default from Draft 4 to Draft 6 / 7 by configuring the loader this way:
99
101
100
102
```java
101
103
SchemaLoader loader =SchemaLoader.builder()
102
104
.schemaJson(yourSchemaJSON)
103
-
.draftV6Support()
105
+
.draftV6Support()// or draftV7Support()
104
106
.build();
105
107
Schema schema = loader.load().build();
106
108
```
@@ -218,7 +220,7 @@ detailed error report, but under some circumstances it is more appropriate to st
218
220
checking the rest of the JSON document. To toggle this fast-failing validation mode
219
221
* you have to explicitly build a `Validator` instance for your schema instead of calling `Schema#validate(input)`
220
222
* you have to call the `failEarly()` method of `ValidatorBuilder`
221
-
223
+
222
224
Example:
223
225
224
226
```java
@@ -267,24 +269,62 @@ System.out.println(input.get("prop")); // prints 1
267
269
If there are some properties missing from `input` which have `"default"` values in the schema, then they will be set by the validator
268
270
during validation.
269
271
270
-
## Format validators
272
+
## readOnly and writeOnly context
271
273
274
+
The library supports the `readOnly` and `writeOnly` keywords which first appeared in Draft 7. If you want to utilize this feature, then before validation you need to tell the vaildator if the
275
+
validation happens in read or write context. Example:
272
276
273
-
Starting from version `1.2.0` the library supports the [`"format"` keyword][draft-fge-json-schema-validation-00 format]
274
-
(which is an optional part of the specification), so you can use the following formats in the schemas:
277
+
schema.json:
278
+
279
+
```json
280
+
{
281
+
"properties": {
282
+
"id": {
283
+
"type": "number",
284
+
"readOnly": true
285
+
}
286
+
}
287
+
}
288
+
```
275
289
276
-
* date-time
277
-
* email
278
-
* hostname
279
-
* ipv4
280
-
* ipv6
281
-
* uri
290
+
Validation code snippet:
291
+
```java
282
292
283
-
If you use the library in Draft 6 mode, then the followings are also supported:
In this case we told the validator that the validation happens in `WRITE` context, and in the input JSON object the `"id"` property appears, which is marked as `"readOnly"` in the schema, therefore this call will throw a `ValidationException`.
301
+
302
+
## Format validators
303
+
304
+
305
+
Starting from version `1.2.0` the library supports the [`"format"` keyword][draft-fge-json-schema-validation-00 format]
306
+
(which is an optional part of the specification).
307
+
308
+
The supported formats vary depending on the schema spec version you use (since the standard formats were introduced in different versions on the validation specification).
309
+
310
+
Here is a compatibility table of te supported standard formats:
0 commit comments