Skip to content

Commit 2d12f78

Browse files
committed
Rename SpecVersion.VersionFlag to Specification.Version and refactor
1 parent e7781cb commit 2d12f78

File tree

164 files changed

+629
-593
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

164 files changed

+629
-593
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.Map.Entry;
2020

2121
import com.fasterxml.jackson.databind.JsonNode;
22-
import com.networknt.schema.SpecVersion.VersionFlag;
22+
import com.networknt.schema.Specification.Version;
2323

2424
/**
2525
* Default {@link JsonMetaSchemaFactory}.
@@ -28,7 +28,7 @@ public class DefaultJsonMetaSchemaFactory implements JsonMetaSchemaFactory {
2828
@Override
2929
public JsonMetaSchema getMetaSchema(String iri, JsonSchemaFactory schemaFactory, SchemaValidatorsConfig config) {
3030
// Is it a well-known dialect?
31-
return SpecVersionDetector.detectOptionalVersion(iri)
31+
return Specification.Version.fromDialectId(iri)
3232
.map(JsonSchemaFactory::checkVersion)
3333
.map(JsonSchemaVersion::getInstance)
3434
.orElseGet(() -> {
@@ -55,9 +55,9 @@ protected JsonMetaSchema.Builder loadMetaSchemaBuilder(String iri, JsonSchemaFac
5555
SchemaValidatorsConfig config) {
5656
JsonSchema schema = schemaFactory.getSchema(SchemaLocation.of(iri), config);
5757
JsonMetaSchema.Builder builder = JsonMetaSchema.builder(iri, schema.getValidationContext().getMetaSchema());
58-
VersionFlag specification = schema.getValidationContext().getMetaSchema().getSpecification();
58+
Version specification = schema.getValidationContext().getMetaSchema().getSpecification();
5959
if (specification != null) {
60-
if (specification.getVersionFlagValue() >= VersionFlag.V201909.getVersionFlagValue()) {
60+
if (specification.getOrder() >= Version.DRAFT_2019_09.getOrder()) {
6161
// Process vocabularies
6262
JsonNode vocabulary = schema.getSchemaNode().get("$vocabulary");
6363
if (vocabulary != null) {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package com.networknt.schema;
1818

1919
import com.fasterxml.jackson.databind.JsonNode;
20-
import com.networknt.schema.SpecVersion.VersionFlag;
20+
import com.networknt.schema.Specification.Version;
2121
import com.networknt.schema.keyword.FormatKeyword;
2222
import com.networknt.schema.keyword.Keyword;
2323
import com.networknt.schema.keyword.KeywordFactory;
@@ -61,7 +61,7 @@ public interface FormatKeywordFactory {
6161
public static class Builder {
6262
private String iri;
6363
private String idKeyword = "$id";
64-
private VersionFlag specification = null;
64+
private Version specification = null;
6565
private final Map<String, Keyword> keywords = new HashMap<>();
6666
private final Map<String, Format> formats = new HashMap<>();
6767
private final Map<String, Boolean> vocabularies = new HashMap<>();
@@ -253,7 +253,7 @@ public Builder vocabularies(Consumer<Map<String, Boolean>> customizer) {
253253
* @param specification the specification
254254
* @return the builder
255255
*/
256-
public Builder specification(VersionFlag specification) {
256+
public Builder specification(Version specification) {
257257
this.specification = specification;
258258
return this;
259259
}
@@ -273,7 +273,7 @@ public JsonMetaSchema build() {
273273
// create builtin keywords with (custom) formats.
274274
Map<String, Keyword> keywords = this.keywords;
275275
if (this.specification != null) {
276-
if (this.specification.getVersionFlagValue() >= SpecVersion.VersionFlag.V201909.getVersionFlagValue()) {
276+
if (this.specification.getOrder() >= Specification.Version.DRAFT_2019_09.getOrder()) {
277277
keywords = new HashMap<>(this.keywords);
278278
for(Entry<String, Boolean> entry : this.vocabularies.entrySet()) {
279279
Vocabulary vocabulary = null;
@@ -326,11 +326,11 @@ public Builder addFormats(Collection<? extends Format> formats) {
326326
private final String idKeyword;
327327
private final Map<String, Keyword> keywords;
328328
private final Map<String, Boolean> vocabularies;
329-
private final VersionFlag specification;
329+
private final Version specification;
330330

331331
private final Builder builder;
332332

333-
JsonMetaSchema(String iri, String idKeyword, Map<String, Keyword> keywords, Map<String, Boolean> vocabularies, VersionFlag specification, Builder builder) {
333+
JsonMetaSchema(String iri, String idKeyword, Map<String, Keyword> keywords, Map<String, Boolean> vocabularies, Version specification, Builder builder) {
334334
if (StringUtils.isBlank(iri)) {
335335
throw new IllegalArgumentException("iri must not be null or blank");
336336
}
@@ -461,7 +461,7 @@ public Map<String, Boolean> getVocabularies() {
461461
return this.vocabularies;
462462
}
463463

464-
public VersionFlag getSpecification() {
464+
public Version getSpecification() {
465465
return this.specification;
466466
}
467467

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
import com.fasterxml.jackson.databind.JsonNode;
3333
import com.fasterxml.jackson.databind.node.ObjectNode;
34-
import com.networknt.schema.SpecVersion.VersionFlag;
34+
import com.networknt.schema.Specification.Version;
3535
import com.networknt.schema.keyword.DiscriminatorValidator;
3636
import com.networknt.schema.keyword.KeywordValidator;
3737
import com.networknt.schema.keyword.TypeValidator;
@@ -40,7 +40,7 @@
4040

4141
/**
4242
* Used for creating a schema with validators for validating inputs. This is
43-
* created using {@link JsonSchemaFactory#getInstance(VersionFlag, Consumer)}
43+
* created using {@link JsonSchemaFactory#getInstance(Version, Consumer)}
4444
* and should be cached for performance.
4545
* <p>
4646
* This is the core of json constraint implementation. It parses json constraint
@@ -51,7 +51,7 @@
5151
* modified.
5252
*/
5353
public class JsonSchema implements Validator {
54-
private static final long V201909_VALUE = VersionFlag.V201909.getVersionFlagValue();
54+
private static final long V201909_VALUE = Version.DRAFT_2019_09.getOrder();
5555
private final String id;
5656

5757
/**
@@ -673,8 +673,8 @@ private List<KeywordValidator> read(JsonNode schemaNode) {
673673
private long activeDialect() {
674674
return this.validationContext
675675
.activeDialect()
676-
.map(VersionFlag::getVersionFlagValue)
677-
.orElse(Long.MAX_VALUE);
676+
.map(Version::getOrder)
677+
.orElse(Integer.MAX_VALUE);
678678
}
679679

680680
/**

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import com.fasterxml.jackson.databind.JsonNode;
2020
import com.fasterxml.jackson.databind.ObjectMapper;
21-
import com.networknt.schema.SpecVersion.VersionFlag;
21+
import com.networknt.schema.Specification.Version;
2222
import com.networknt.schema.resource.DefaultSchemaLoader;
2323
import com.networknt.schema.resource.SchemaLoader;
2424
import com.networknt.schema.resource.SchemaLoaders;
@@ -43,7 +43,7 @@
4343

4444
/**
4545
* Factory for building {@link JsonSchema} instances. The factory should be
46-
* typically be created using {@link #getInstance(VersionFlag, Consumer)} and
46+
* typically be created using {@link #getInstance(Version, Consumer)} and
4747
* should be cached for performance.
4848
* <p>
4949
* JsonSchemaFactory instances are thread-safe provided its configuration is not
@@ -251,7 +251,7 @@ public static Builder builder() {
251251
* @param versionFlag the default dialect
252252
* @return the factory
253253
*/
254-
public static JsonSchemaFactory getInstance(SpecVersion.VersionFlag versionFlag) {
254+
public static JsonSchemaFactory getInstance(Specification.Version versionFlag) {
255255
return getInstance(versionFlag, null);
256256
}
257257

@@ -263,7 +263,7 @@ public static JsonSchemaFactory getInstance(SpecVersion.VersionFlag versionFlag)
263263
* @param customizer to customize the factory
264264
* @return the factory
265265
*/
266-
public static JsonSchemaFactory getInstance(SpecVersion.VersionFlag versionFlag,
266+
public static JsonSchemaFactory getInstance(Specification.Version versionFlag,
267267
Consumer<JsonSchemaFactory.Builder> customizer) {
268268
JsonSchemaVersion jsonSchemaVersion = checkVersion(versionFlag);
269269
JsonMetaSchema metaSchema = jsonSchemaVersion.getInstance();
@@ -283,14 +283,14 @@ public static JsonSchemaFactory getInstance(SpecVersion.VersionFlag versionFlag,
283283
* @param versionFlag the schema dialect
284284
* @return the version
285285
*/
286-
public static JsonSchemaVersion checkVersion(SpecVersion.VersionFlag versionFlag){
286+
public static JsonSchemaVersion checkVersion(Specification.Version versionFlag){
287287
if (null == versionFlag) return null;
288288
switch (versionFlag) {
289-
case V202012: return new Version202012();
290-
case V201909: return new Version201909();
291-
case V7: return new Version7();
292-
case V6: return new Version6();
293-
case V4: return new Version4();
289+
case DRAFT_2020_12: return new Version202012();
290+
case DRAFT_2019_09: return new Version201909();
291+
case DRAFT_7: return new Version7();
292+
case DRAFT_6: return new Version6();
293+
case DRAFT_4: return new Version4();
294294
default: throw new IllegalArgumentException("Unsupported value" + versionFlag);
295295
}
296296
}
@@ -822,8 +822,8 @@ private boolean isYaml(final SchemaLocation schemaUri) {
822822
*/
823823
static protected String normalizeMetaSchemaUri(String id) {
824824
boolean found = false;
825-
for (VersionFlag flag : SpecVersion.VersionFlag.values()) {
826-
if(flag.getId().equals(id)) {
825+
for (Version flag : Specification.Version.values()) {
826+
if(flag.getDialectId().equals(id)) {
827827
found = true;
828828
break;
829829
}

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

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2020 Network New Technologies Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.networknt.schema;
17+
18+
import java.util.Optional;
19+
20+
/**
21+
* The JSON Schema specification which defines the standard dialects.
22+
*/
23+
public class Specification {
24+
25+
/**
26+
* The JSON Schema specification version.
27+
*/
28+
public enum Version {
29+
/**
30+
* Draft 4.
31+
*/
32+
DRAFT_4(4, SchemaId.V4),
33+
/**
34+
* Draft 6.
35+
*/
36+
DRAFT_6(6, SchemaId.V6),
37+
/**
38+
* Draft 7.
39+
*/
40+
DRAFT_7(7, SchemaId.V7),
41+
/**
42+
* Draft 2019-09.
43+
*/
44+
DRAFT_2019_09(8, SchemaId.V201909),
45+
/**
46+
* Draft 2020-12.
47+
*/
48+
DRAFT_2020_12(9, SchemaId.V202012);
49+
50+
private final int order;
51+
private final String dialectId;
52+
53+
Version(int order, String dialectId) {
54+
this.order = order;
55+
this.dialectId = dialectId;
56+
}
57+
58+
/**
59+
* Gets the dialect id used for the $schema keyword. The dialect id is an IRI
60+
* that identifies the meta schema used to validate the dialect.
61+
*
62+
* @return the dialect id
63+
*/
64+
public String getDialectId() {
65+
return this.dialectId;
66+
}
67+
68+
/**
69+
* Gets the unique release order of the specification version used that
70+
* indicates when the specification was released. Lower numbers indicate the
71+
* specification was released earlier.
72+
*
73+
* @return the order when the specification was released
74+
*/
75+
public int getOrder() {
76+
return this.order;
77+
}
78+
79+
/**
80+
* Gets the specification version that matches the dialect id indicated by
81+
* $schema keyword. The dialect id is an IRI that identifies the meta schema
82+
* used to validate the dialect.
83+
*
84+
* @param dialectId the dialect id specified by $schema keyword
85+
* @return the specification version if it matches the dialect id
86+
*/
87+
public static Optional<Version> fromDialectId(String dialectId) {
88+
for (Version version : Version.values()) {
89+
if (version.dialectId.equals(dialectId)) {
90+
return Optional.of(version);
91+
}
92+
}
93+
return Optional.empty();
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)