Skip to content

Commit f3ee10a

Browse files
committed
bumping version number to 1.8.0; updating readme with draft-7 docs
1 parent df06811 commit f3ee10a

File tree

4 files changed

+65
-25
lines changed

4 files changed

+65
-25
lines changed

README.md

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
* [Maven installation](#maven-installation)
77
* [Java7 version](#java7-version)
88
* [Quickstart](#quickstart)
9-
* [Draft 4 or Draft 6?](#draft-4-or-draft-6)
9+
* [Draft 4 or Draft 6 or Draft 7?](#draft-4-or-draft-6-or-draft-7)
1010
* [Investigating failures](#investigating-failures)
1111
* [JSON report of the failures](#json-report-of-the-failures)
1212
* [Eary failure mode](#early-failure-mode)
1313
* [Default values](#default-values)
14+
* [readOnly and writeOnly context](#readonly-and-writeonly-context)
1415
* [Format validators](#format-validators)
1516
* [Example](#example)
1617
* [Resolution scopes](#resolution-scopes)
1718

1819
<a href="http://jetbrains.com"><img src="./jetbrains-logo.png" /></a> Supported by JetBrains.
1920

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.
2122
It uses the [org.json API](http://stleary.github.io/JSON-java/) (created by Douglas Crockford) for representing JSON data.
2223

2324
# When to use this library?
@@ -27,7 +28,7 @@ But - as you may have already discovered - there is also an [other Java implemen
2728
of the JSON Schema specification. So here are some advices about which one to use:
2829
* 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
2930
* 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.
3132
* if you want to use anything else for handling JSON (like GSON or javax.json), then you are in a little trouble, since
3233
currently there is no schema validation library backed by these libraries. It means that you will have to parse the JSON
3334
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:
4243
<dependency>
4344
<groupId>com.github.everit-org.json-schema</groupId>
4445
<artifactId>org.everit.json.schema</artifactId>
45-
<version>1.7.0</version>
46+
<version>1.8.0</version>
4647
</dependency>
4748
...
4849
<repositories>
@@ -83,24 +84,25 @@ try (InputStream inputStream = getClass().getResourceAsStream("/path/to/your/sch
8384
}
8485
```
8586

86-
## Draft 4 or draft 6?
87+
## Draft 4, Draft 6 or Draft 7?
8788

88-
JSON Schema has currently 3 major releases, Draft 3, Draft 4 and 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).
8990
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.
9091

9192
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.
9293

9394
Quick reference:
9495
* if there is `"$schema": "http://json-schema.org/draft-04/schema"` in the schema root, then Draft 4 will be used
9596
* 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
9698
* if none of these is found then Draft 4 will be assumed as default
9799

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:
99101

100102
```java
101103
SchemaLoader loader = SchemaLoader.builder()
102104
.schemaJson(yourSchemaJSON)
103-
.draftV6Support()
105+
.draftV6Support() // or draftV7Support()
104106
.build();
105107
Schema schema = loader.load().build();
106108
```
@@ -218,7 +220,7 @@ detailed error report, but under some circumstances it is more appropriate to st
218220
checking the rest of the JSON document. To toggle this fast-failing validation mode
219221
* you have to explicitly build a `Validator` instance for your schema instead of calling `Schema#validate(input)`
220222
* you have to call the `failEarly()` method of `ValidatorBuilder`
221-
223+
222224
Example:
223225

224226
```java
@@ -267,24 +269,62 @@ System.out.println(input.get("prop")); // prints 1
267269
If there are some properties missing from `input` which have `"default"` values in the schema, then they will be set by the validator
268270
during validation.
269271

270-
## Format validators
272+
## readOnly and writeOnly context
271273

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:
272276

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+
```
275289

276-
* date-time
277-
* email
278-
* hostname
279-
* ipv4
280-
* ipv6
281-
* uri
290+
Validation code snippet:
291+
```java
282292

283-
If you use the library in Draft 6 mode, then the followings are also supported:
293+
Validator validator = Validator.builder()
294+
.readWriteContext(ReadWriteContext.WRITE)
295+
.build();
296+
297+
validator.performValidation(schema, new JSONObject("{\"id\":42}"));
298+
```
299+
300+
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:
311+
312+
| | Draft 4 | Draft 6 | Draft 7 |
313+
----------------------------------------------------------------------------
314+
| date-time | :white_check_mark: | :white_check_mark: | :white_check_mark: |
315+
| email | :white_check_mark: | :white_check_mark: | :white_check_mark: |
316+
| hostname | :white_check_mark: | :white_check_mark: | :white_check_mark: |
317+
| ipv4 | :white_check_mark: | :white_check_mark: | :white_check_mark: |
318+
| ipv6 | :white_check_mark: | :white_check_mark: | :white_check_mark: |
319+
| uri | :white_check_mark: | :white_check_mark: | :white_check_mark: |
320+
| uri-reference | | :white_check_mark: | :white_check_mark: |
321+
| uri-template | | :white_check_mark: | :white_check_mark: |
322+
| json-pointer | | :white_check_mark: | :white_check_mark: |
323+
| date | | | :white_check_mark: |
324+
| time | | | :white_check_mark: |
325+
| regex | | | :white_check_mark: |
326+
| relative-json-pointer| | | :white_check_mark: |
284327

285-
* uri-reference
286-
* uri-template
287-
* json-pointer
288328

289329
The library also supports adding custom format validators. To use a custom validator basically you have to
290330

core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<modelVersion>4.0.0</modelVersion>
2222
<groupId>org.everit.json</groupId>
2323
<artifactId>org.everit.json.schema</artifactId>
24-
<version>1.7.0</version>
24+
<version>1.8.0</version>
2525
<packaging>bundle</packaging>
2626
<properties>
2727
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>org.everit.json</groupId>
88
<artifactId>org.everit.json.schema.parent</artifactId>
9-
<version>1.7.0</version>
9+
<version>1.8.0</version>
1010

1111
<packaging>pom</packaging>
1212

tests/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<parent>
2525
<groupId>org.everit.json</groupId>
2626
<artifactId>org.everit.json.schema.parent</artifactId>
27-
<version>1.7.0</version>
27+
<version>1.8.0</version>
2828
</parent>
2929

3030
<artifactId>org.everit.json.schema.tests</artifactId>

0 commit comments

Comments
 (0)