Skip to content

Update the specversion.md and pom.xml #613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 37 additions & 31 deletions doc/specversion.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The library supports V4, V6, V7, and V2019-09 JSON schema specifications. By default, V4 is used for backward compatibility.
The library supports V4, V6, V7, V2019-09 and V2020-12 JSON schema specifications. By default, V4 is used for backward compatibility.

### For Users

Expand Down Expand Up @@ -48,6 +48,17 @@ or with default configuration
JsonSchemaFactory validatorFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909));
```

#### To create a draft 2020-12 JsonSchemaFactory

```java
ObjectMapper mapper = new ObjectMapper();
JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012)).objectMapper(mapper).build();
```
or with default configuration
```java
JsonSchemaFactory validatorFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012));
```

#### To create a JsonSchemaFactory, automatically detecting schema version

```java
Expand All @@ -74,43 +85,45 @@ public enum VersionFlag {
V4(1<<0),
V6(1<<1),
V7(1<<2),
V201909(1<<3);
V201909(1<<3),
V202012(1<<4);

```

In the long value, we are using 4 bits now as we are supporting 4 versions at the moment.
In the long value, we are using 5 bits now as we are supporting 5 versions at the moment.

V4 -> 0001 -> 1
V6 -> 0010 -> 2
V7 -> 0100 -> 4
V201909 -> 1000 -> 8
V4 -> 00001 -> 1
V6 -> 00010 -> 2
V7 -> 00100 -> 4
V201909 -> 01000 -> 8
V202012 -> 10000 --> 16

If we have a new version added, it should be

V202009 -> 10000 -> 16
V202209 -> 100000 -> 32

#### ValidatorTypeCode

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

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

For example.

```
MAXIMUM("maximum", "1011", new MessageFormat("{0}: must have a maximum value of {1}"), MaximumValidator.class, 15),
MAXIMUM("maximum", "1011", new MessageFormat("{0}: must have a maximum value of {1}"), MaximumValidator.class, 31),
```

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

```
IF_THEN_ELSE("if", "1037", null, IfValidator.class, 12), // V7|V201909 1100
IF_THEN_ELSE("if", "1037", null, IfValidator.class, 28), // V7|V201909|V202012 11100
```

For exclusiveMaximum, it was introduced from V6

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

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

#### JsonMetaSchema

We have created four different static classes V4, V6, V7, and V201909 to build different JsonMetaSchema instances.
We have created four different static classes V4, V6, V7, V201909 and V202012 to build different JsonMetaSchema instances.

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

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

public static JsonSchemaFactory getInstance(SpecVersion.VersionFlag versionFlag) {
JsonMetaSchema metaSchema = null;
switch (versionFlag) {
case V201909:
metaSchema = JsonMetaSchema.getV201909();
break;
case V7:
metaSchema = JsonMetaSchema.getV7();
break;
case V6:
metaSchema = JsonMetaSchema.getV6();
break;
case V4:
metaSchema = JsonMetaSchema.getV4();
break;
}
JsonSchemaVersion jsonSchemaVersion = checkVersion(versionFlag);
JsonMetaSchema metaSchema = jsonSchemaVersion.getInstance();
return builder()
.defaultMetaSchemaURI(metaSchema.getUri())
.addMetaSchema(metaSchema)
Expand All @@ -177,7 +177,10 @@ public static SpecVersion.VersionFlag detect(JsonNode jsonNode) {
if (!jsonNode.has(SCHEMA_TAG))
throw new JsonSchemaException("Schema tag not present");

String schemaUri = JsonSchemaFactory.normalizeMetaSchemaUri(jsonNode.get(SCHEMA_TAG).asText());
final boolean forceHttps = true;
final boolean removeEmptyFragmentSuffix = true;

String schemaUri = JsonSchemaFactory.normalizeMetaSchemaUri(jsonNode.get(SCHEMA_TAG).asText(), forceHttps, removeEmptyFragmentSuffix);
if (schemaUri.equals(JsonMetaSchema.getV4().getUri()))
return SpecVersion.VersionFlag.V4;
else if (schemaUri.equals(JsonMetaSchema.getV6().getUri()))
Expand All @@ -186,21 +189,24 @@ public static SpecVersion.VersionFlag detect(JsonNode jsonNode) {
return SpecVersion.VersionFlag.V7;
else if (schemaUri.equals(JsonMetaSchema.getV201909().getUri()))
return SpecVersion.VersionFlag.V201909;
else if (schemaUri.equals(JsonMetaSchema.getV202012().getUri()))
return SpecVersion.VersionFlag.V202012;
else
throw new JsonSchemaException("Unrecognizable schema");
}
```

### For Testers

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

The existing JsonSchemaTest has been renamed to V4JsonSchemaTest, and the following test classes are added.

```
V6JsonSchemaTest
V7JsonSchemaTest
V201909JsonSchemaTest
V202012JsonSchemaTest
```

These new test classes are not completed yet, and only some sample test cases are added.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<artifactId>json-schema-validator</artifactId>
<version>1.0.73</version>
<packaging>bundle</packaging>
<description>A json schema validator that supports draft v4, v6, v7 and v2019-09</description>
<description>A json schema validator that supports draft v4, v6, v7, v2019-09 and v2020-12</description>
<url>https://github.com/networknt/json-schema-validator</url>
<name>JsonSchemaValidator</name>
<developers>
Expand Down