Skip to content

Commit 3cf9bb6

Browse files
fduttonFaron Dutton
andauthored
Adds support for cross-draft validation (#779)
Resolves #778 Co-authored-by: Faron Dutton <[email protected]>
1 parent 1834353 commit 3cf9bb6

File tree

4 files changed

+41
-59
lines changed

4 files changed

+41
-59
lines changed

src/main/java/com/networknt/schema/JsonSchemaFactory.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -293,25 +293,15 @@ public static JsonSchemaFactory getInstance(SpecVersion.VersionFlag versionFlag)
293293
}
294294

295295
public static JsonSchemaVersion checkVersion(SpecVersion.VersionFlag versionFlag){
296-
JsonSchemaVersion jsonSchemaVersion = null;
296+
if (null == versionFlag) return null;
297297
switch (versionFlag) {
298-
case V202012:
299-
jsonSchemaVersion = new Version202012();
300-
break;
301-
case V201909:
302-
jsonSchemaVersion = new Version201909();
303-
break;
304-
case V7:
305-
jsonSchemaVersion = new Version7();
306-
break;
307-
case V6:
308-
jsonSchemaVersion = new Version6();
309-
break;
310-
case V4:
311-
jsonSchemaVersion = new Version4();
312-
break;
298+
case V202012: return new Version202012();
299+
case V201909: return new Version201909();
300+
case V7: return new Version7();
301+
case V6: return new Version6();
302+
case V4: return new Version4();
303+
default: throw new IllegalArgumentException("Unsupported value" + versionFlag);
313304
}
314-
return jsonSchemaVersion;
315305
}
316306

317307
public static Builder builder(final JsonSchemaFactory blueprint) {
@@ -351,13 +341,18 @@ private JsonMetaSchema findMetaSchemaForSchema(final JsonNode schemaNode) {
351341
throw new JsonSchemaException("Unknown MetaSchema: " + uriNode.toString());
352342
}
353343
final String uri = uriNode == null || uriNode.isNull() ? defaultMetaSchemaURI : normalizeMetaSchemaUri(uriNode.textValue(), forceHttps, removeEmptyFragmentSuffix);
354-
final JsonMetaSchema jsonMetaSchema = jsonMetaSchemas.get(uri);
355-
if (jsonMetaSchema == null) {
356-
throw new JsonSchemaException("Unknown MetaSchema: " + uri);
357-
}
344+
final JsonMetaSchema jsonMetaSchema = jsonMetaSchemas.computeIfAbsent(uri, this::fromId);
358345
return jsonMetaSchema;
359346
}
360347

348+
private JsonMetaSchema fromId(String id) {
349+
// Is it a well-known dialect?
350+
return SpecVersionDetector.detectOptionalVersion(id)
351+
.map(JsonSchemaFactory::checkVersion)
352+
.map(JsonSchemaVersion::getInstance)
353+
.orElseThrow(() -> new JsonSchemaException("Unknown MetaSchema: " + id));
354+
}
355+
361356
/**
362357
* @return A shared {@link URI} factory that is used for creating the URI references in schemas.
363358
*/

src/main/java/com/networknt/schema/SpecVersion.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,40 @@
1515
*/
1616
package com.networknt.schema;
1717

18+
import java.util.Optional;
19+
1820
public class SpecVersion {
1921

2022
public enum VersionFlag {
2123

22-
V4(1 << 0),
23-
V6(1 << 1),
24-
V7(1 << 2),
25-
V201909(1 << 3),
26-
V202012(1 << 4);
24+
V4(1 << 0, "https://json-schema.org/draft-04/schema"),
25+
V6(1 << 1, "https://json-schema.org/draft-06/schema"),
26+
V7(1 << 2, "https://json-schema.org/draft-07/schema"),
27+
V201909(1 << 3, "https://json-schema.org/draft/2019-09/schema"),
28+
V202012(1 << 4, "https://json-schema.org/draft/2020-12/schema");
2729

2830

2931
private final long versionFlagValue;
32+
private final String id;
3033

31-
VersionFlag(long versionFlagValue) {
34+
VersionFlag(long versionFlagValue, String id) {
3235
this.versionFlagValue = versionFlagValue;
36+
this.id = id;
37+
}
38+
39+
public String getId() {
40+
return this.id;
3341
}
3442

3543
public long getVersionFlagValue() {
36-
return versionFlagValue;
44+
return this.versionFlagValue;
45+
}
46+
47+
public static Optional<VersionFlag> fromId(String id) {
48+
for (VersionFlag v: VersionFlag.values()) {
49+
if (v.id.equals(id)) return Optional.of(v);
50+
}
51+
return Optional.empty();
3752
}
3853
}
3954

src/main/java/com/networknt/schema/SpecVersionDetector.java

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -65,37 +65,13 @@ public static Optional<VersionFlag> detectOptionalVersion(JsonNode jsonNode) {
6565
String schemaUri = JsonSchemaFactory.normalizeMetaSchemaUri(schemaTagValue, forceHttps,
6666
removeEmptyFragmentSuffix);
6767

68-
VersionFlag version = detect(schemaUri);
69-
if (null == version) {
70-
throw new JsonSchemaException("'" + schemaTagValue + "' is unrecognizable schema");
71-
}
72-
return version;
68+
return VersionFlag.fromId(schemaUri)
69+
.orElseThrow(() -> new JsonSchemaException("'" + schemaTagValue + "' is unrecognizable schema"));
7370
});
7471
}
7572

7673
public static Optional<VersionFlag> detectOptionalVersion(String schemaUri) {
77-
return Optional.ofNullable(detect(schemaUri));
78-
}
79-
80-
private static VersionFlag detect(String schemaUri) {
81-
if (null != schemaUri) {
82-
if (schemaUri.equals(JsonMetaSchema.getV4().getUri())) {
83-
return VersionFlag.V4;
84-
}
85-
if (schemaUri.equals(JsonMetaSchema.getV6().getUri())) {
86-
return VersionFlag.V6;
87-
}
88-
if (schemaUri.equals(JsonMetaSchema.getV7().getUri())) {
89-
return VersionFlag.V7;
90-
}
91-
if (schemaUri.equals(JsonMetaSchema.getV201909().getUri())) {
92-
return VersionFlag.V201909;
93-
}
94-
if (schemaUri.equals(JsonMetaSchema.getV202012().getUri())) {
95-
return VersionFlag.V202012;
96-
}
97-
}
98-
return null;
74+
return VersionFlag.fromId(schemaUri);
9975
}
10076

10177
}

src/test/java/com/networknt/schema/JsonSchemaTestSuiteTest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ private void disableV202012Tests() {
7575
this.disabled.add(Paths.get("src/test/suite/tests/draft2020-12/defs.json"));
7676
this.disabled.add(Paths.get("src/test/suite/tests/draft2020-12/dynamicRef.json"));
7777
this.disabled.add(Paths.get("src/test/suite/tests/draft2020-12/id.json"));
78-
this.disabled.add(Paths.get("src/test/suite/tests/draft2020-12/optional/cross-draft.json"));
7978
this.disabled.add(Paths.get("src/test/suite/tests/draft2020-12/optional/format-assertion.json"));
8079
this.disabled.add(Paths.get("src/test/suite/tests/draft2020-12/ref.json"));
8180
this.disabled.add(Paths.get("src/test/suite/tests/draft2020-12/refRemote.json"));
@@ -86,7 +85,6 @@ private void disableV201909Tests() {
8685
this.disabled.add(Paths.get("src/test/suite/tests/draft2019-09/anchor.json"));
8786
this.disabled.add(Paths.get("src/test/suite/tests/draft2019-09/defs.json"));
8887
this.disabled.add(Paths.get("src/test/suite/tests/draft2019-09/id.json"));
89-
this.disabled.add(Paths.get("src/test/suite/tests/draft2019-09/optional/cross-draft.json"));
9088
this.disabled.add(Paths.get("src/test/suite/tests/draft2019-09/recursiveRef.json"));
9189
this.disabled.add(Paths.get("src/test/suite/tests/draft2019-09/ref.json"));
9290
this.disabled.add(Paths.get("src/test/suite/tests/draft2019-09/refRemote.json"));
@@ -97,13 +95,11 @@ private void disableV7Tests() {
9795
this.disabled.add(Paths.get("src/test/suite/tests/draft7/anchor.json"));
9896
this.disabled.add(Paths.get("src/test/suite/tests/draft7/defs.json"));
9997
this.disabled.add(Paths.get("src/test/suite/tests/draft7/optional/content.json"));
100-
this.disabled.add(Paths.get("src/test/suite/tests/draft7/optional/cross-draft.json"));
10198
this.disabled.add(Paths.get("src/test/suite/tests/draft7/ref.json"));
10299
this.disabled.add(Paths.get("src/test/suite/tests/draft7/refRemote.json"));
103100
}
104101

105102
private void disableV6Tests() {
106-
this.disabled.add(Paths.get("src/test/suite/tests/draft6/optional/format.json"));
107103
this.disabled.add(Paths.get("src/test/suite/tests/draft6/ref.json"));
108104
this.disabled.add(Paths.get("src/test/suite/tests/draft6/refRemote.json"));
109105
}

0 commit comments

Comments
 (0)