Skip to content

Commit f69ea6c

Browse files
Update the specversion.md and pom.xml (#613)
Co-authored-by: S. Tuncer Erdogan <tuncer [at] gmail.com>
1 parent 88ce022 commit f69ea6c

File tree

2 files changed

+38
-32
lines changed

2 files changed

+38
-32
lines changed

doc/specversion.md

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The library supports V4, V6, V7, and V2019-09 JSON schema specifications. By default, V4 is used for backward compatibility.
1+
The library supports V4, V6, V7, V2019-09 and V2020-12 JSON schema specifications. By default, V4 is used for backward compatibility.
22

33
### For Users
44

@@ -48,6 +48,17 @@ or with default configuration
4848
JsonSchemaFactory validatorFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909));
4949
```
5050

51+
#### To create a draft 2020-12 JsonSchemaFactory
52+
53+
```java
54+
ObjectMapper mapper = new ObjectMapper();
55+
JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012)).objectMapper(mapper).build();
56+
```
57+
or with default configuration
58+
```java
59+
JsonSchemaFactory validatorFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012));
60+
```
61+
5162
#### To create a JsonSchemaFactory, automatically detecting schema version
5263

5364
```java
@@ -74,43 +85,45 @@ public enum VersionFlag {
7485
V4(1<<0),
7586
V6(1<<1),
7687
V7(1<<2),
77-
V201909(1<<3);
88+
V201909(1<<3),
89+
V202012(1<<4);
7890

7991
```
8092

81-
In the long value, we are using 4 bits now as we are supporting 4 versions at the moment.
93+
In the long value, we are using 5 bits now as we are supporting 5 versions at the moment.
8294

83-
V4 -> 0001 -> 1
84-
V6 -> 0010 -> 2
85-
V7 -> 0100 -> 4
86-
V201909 -> 1000 -> 8
95+
V4 -> 00001 -> 1
96+
V6 -> 00010 -> 2
97+
V7 -> 00100 -> 4
98+
V201909 -> 01000 -> 8
99+
V202012 -> 10000 --> 16
87100

88101
If we have a new version added, it should be
89102

90-
V202009 -> 10000 -> 16
103+
V202209 -> 100000 -> 32
91104

92105
#### ValidatorTypeCode
93106

94107
A new field versionCode is added to indicate which version the validator is supported.
95108

96-
For most of the validators, the version code should be 15, which is 1111. This means the validator will be loaded for every version of the specification.
109+
For most of the validators, the version code should be 31, which is 11111. This means the validator will be loaded for every version of the specification.
97110

98111
For example.
99112

100113
```
101-
MAXIMUM("maximum", "1011", new MessageFormat("{0}: must have a maximum value of {1}"), MaximumValidator.class, 15),
114+
MAXIMUM("maximum", "1011", new MessageFormat("{0}: must have a maximum value of {1}"), MaximumValidator.class, 31),
102115
```
103116

104-
Since if-then-else was introduced in the V7, it only works for V7 and V2019-09
117+
Since if-then-else was introduced in the V7, it only works for V7, V2019-09 and V2020-12
105118

106119
```
107-
IF_THEN_ELSE("if", "1037", null, IfValidator.class, 12), // V7|V201909 1100
120+
IF_THEN_ELSE("if", "1037", null, IfValidator.class, 28), // V7|V201909|V202012 11100
108121
```
109122

110123
For exclusiveMaximum, it was introduced from V6
111124

112125
```
113-
EXCLUSIVE_MAXIMUM("exclusiveMaximum", "1038", new MessageFormat("{0}: must have a exclusive maximum value of {1}"), ExclusiveMaximumValidator.class, 14), // V6|V7|V201909
126+
EXCLUSIVE_MAXIMUM("exclusiveMaximum", "1038", new MessageFormat("{0}: must have a exclusive maximum value of {1}"), ExclusiveMaximumValidator.class, 30), // V6|V7|V201909|V202012
114127
```
115128

116129
The getNonFormatKeywords method is updated to accept a SpecVersion.VersionFlag so that only the keywords supported by the specification will be loaded.
@@ -129,7 +142,7 @@ public static List<ValidatorTypeCode> getNonFormatKeywords(SpecVersion.VersionFl
129142

130143
#### JsonMetaSchema
131144

132-
We have created four different static classes V4, V6, V7, and V201909 to build different JsonMetaSchema instances.
145+
We have created four different static classes V4, V6, V7, V201909 and V202012 to build different JsonMetaSchema instances.
133146

134147
For the BUILDIN_FORMATS, there is a common section, and each static class has its version-specific BUILDIN_FORMATS section.
135148

@@ -144,21 +157,8 @@ public static JsonSchemaFactory getInstance() {
144157
}
145158

146159
public static JsonSchemaFactory getInstance(SpecVersion.VersionFlag versionFlag) {
147-
JsonMetaSchema metaSchema = null;
148-
switch (versionFlag) {
149-
case V201909:
150-
metaSchema = JsonMetaSchema.getV201909();
151-
break;
152-
case V7:
153-
metaSchema = JsonMetaSchema.getV7();
154-
break;
155-
case V6:
156-
metaSchema = JsonMetaSchema.getV6();
157-
break;
158-
case V4:
159-
metaSchema = JsonMetaSchema.getV4();
160-
break;
161-
}
160+
JsonSchemaVersion jsonSchemaVersion = checkVersion(versionFlag);
161+
JsonMetaSchema metaSchema = jsonSchemaVersion.getInstance();
162162
return builder()
163163
.defaultMetaSchemaURI(metaSchema.getUri())
164164
.addMetaSchema(metaSchema)
@@ -177,7 +177,10 @@ public static SpecVersion.VersionFlag detect(JsonNode jsonNode) {
177177
if (!jsonNode.has(SCHEMA_TAG))
178178
throw new JsonSchemaException("Schema tag not present");
179179

180-
String schemaUri = JsonSchemaFactory.normalizeMetaSchemaUri(jsonNode.get(SCHEMA_TAG).asText());
180+
final boolean forceHttps = true;
181+
final boolean removeEmptyFragmentSuffix = true;
182+
183+
String schemaUri = JsonSchemaFactory.normalizeMetaSchemaUri(jsonNode.get(SCHEMA_TAG).asText(), forceHttps, removeEmptyFragmentSuffix);
181184
if (schemaUri.equals(JsonMetaSchema.getV4().getUri()))
182185
return SpecVersion.VersionFlag.V4;
183186
else if (schemaUri.equals(JsonMetaSchema.getV6().getUri()))
@@ -186,21 +189,24 @@ public static SpecVersion.VersionFlag detect(JsonNode jsonNode) {
186189
return SpecVersion.VersionFlag.V7;
187190
else if (schemaUri.equals(JsonMetaSchema.getV201909().getUri()))
188191
return SpecVersion.VersionFlag.V201909;
192+
else if (schemaUri.equals(JsonMetaSchema.getV202012().getUri()))
193+
return SpecVersion.VersionFlag.V202012;
189194
else
190195
throw new JsonSchemaException("Unrecognizable schema");
191196
}
192197
```
193198

194199
### For Testers
195200

196-
In the test resource folder, we have created and copied all draft version's test suite. They are located in draft4, draft6, draft7, and draft2019-09 folder.
201+
In the test resource folder, we have created and copied all draft version's test suite. They are located in draft4, draft6, draft7, draft2019-09 and draft2020-12 folders.
197202
198203
The existing JsonSchemaTest has been renamed to V4JsonSchemaTest, and the following test classes are added.
199204
200205
```
201206
V6JsonSchemaTest
202207
V7JsonSchemaTest
203208
V201909JsonSchemaTest
209+
V202012JsonSchemaTest
204210
```
205211
206212
These new test classes are not completed yet, and only some sample test cases are added.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<artifactId>json-schema-validator</artifactId>
2323
<version>1.0.73</version>
2424
<packaging>bundle</packaging>
25-
<description>A json schema validator that supports draft v4, v6, v7 and v2019-09</description>
25+
<description>A json schema validator that supports draft v4, v6, v7, v2019-09 and v2020-12</description>
2626
<url>https://github.com/networknt/json-schema-validator</url>
2727
<name>JsonSchemaValidator</name>
2828
<developers>

0 commit comments

Comments
 (0)