Skip to content

Commit c7940b5

Browse files
committed
Refactor dialect and add dialects
1 parent 5a95fa6 commit c7940b5

Some content is hidden

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

45 files changed

+268
-155
lines changed

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

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

2121
import com.fasterxml.jackson.databind.JsonNode;
2222
import com.networknt.schema.Specification.Version;
23+
import com.networknt.schema.dialect.Dialect;
2324

2425
/**
2526
* Default {@link JsonMetaSchemaFactory}.
@@ -30,7 +31,6 @@ public Dialect getMetaSchema(String iri, JsonSchemaFactory schemaFactory, Schema
3031
// Is it a well-known dialect?
3132
return Specification.Version.fromDialectId(iri)
3233
.map(JsonSchemaFactory::checkVersion)
33-
.map(JsonSchemaVersion::getInstance)
3434
.orElseGet(() -> {
3535
// Custom meta schema
3636
return loadMetaSchema(iri, schemaFactory, config);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.networknt.schema;
1717

18+
import com.networknt.schema.dialect.Dialect;
19+
1820
/**
1921
* A {@link JsonMetaSchemaFactory} that does not meta-schemas that aren't
2022
* explicitly configured in the {@link JsonSchemaFactory}.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.networknt.schema;
1717

18+
import com.networknt.schema.dialect.Dialect;
19+
1820
/**
1921
* Factory for {@link Dialect}.
2022
*/

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import com.fasterxml.jackson.databind.JsonNode;
2020
import com.fasterxml.jackson.databind.ObjectMapper;
2121
import com.networknt.schema.Specification.Version;
22+
import com.networknt.schema.dialect.Dialect;
23+
import com.networknt.schema.dialect.DialectId;
24+
import com.networknt.schema.dialect.Dialects;
2225
import com.networknt.schema.resource.DefaultSchemaLoader;
2326
import com.networknt.schema.resource.SchemaLoader;
2427
import com.networknt.schema.resource.SchemaLoaders;
@@ -265,10 +268,9 @@ public static JsonSchemaFactory getInstance(Specification.Version versionFlag) {
265268
*/
266269
public static JsonSchemaFactory getInstance(Specification.Version versionFlag,
267270
Consumer<JsonSchemaFactory.Builder> customizer) {
268-
JsonSchemaVersion jsonSchemaVersion = checkVersion(versionFlag);
269-
Dialect metaSchema = jsonSchemaVersion.getInstance();
270-
JsonSchemaFactory.Builder builder = builder().defaultMetaSchemaIri(metaSchema.getIri())
271-
.metaSchema(metaSchema);
271+
Dialect dialect = checkVersion(versionFlag);
272+
JsonSchemaFactory.Builder builder = builder().defaultMetaSchemaIri(dialect.getIri())
273+
.metaSchema(dialect);
272274
if (customizer != null) {
273275
customizer.accept(builder);
274276
}
@@ -280,18 +282,18 @@ public static JsonSchemaFactory getInstance(Specification.Version versionFlag,
280282
* <p>
281283
* This throws an {@link IllegalArgumentException} for an unsupported value.
282284
*
283-
* @param versionFlag the schema dialect
285+
* @param version the schema specification version
284286
* @return the version
285287
*/
286-
public static JsonSchemaVersion checkVersion(Specification.Version versionFlag){
287-
if (null == versionFlag) return null;
288-
switch (versionFlag) {
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();
294-
default: throw new IllegalArgumentException("Unsupported value" + versionFlag);
288+
public static Dialect checkVersion(Specification.Version version){
289+
if (null == version) return null;
290+
switch (version) {
291+
case DRAFT_2020_12: return Dialects.getDraft202012();
292+
case DRAFT_2019_09: return Dialects.getDraft201909();
293+
case DRAFT_7: return Dialects.getDraft7();
294+
case DRAFT_6: return Dialects.getDraft6();
295+
case DRAFT_4: return Dialects.getDraft4();
296+
default: throw new IllegalArgumentException("Unsupported value" + version);
295297
}
296298
}
297299

@@ -384,16 +386,16 @@ private JsonSchema doCreate(ValidationContext validationContext, SchemaLocation
384386
* @return the validation context to use
385387
*/
386388
private ValidationContext withMetaSchema(ValidationContext validationContext, JsonNode schemaNode) {
387-
Dialect metaSchema = getMetaSchema(schemaNode, validationContext.getConfig());
388-
if (metaSchema != null && !metaSchema.getIri().equals(validationContext.getMetaSchema().getIri())) {
389+
Dialect dialect = getMetaSchema(schemaNode, validationContext.getConfig());
390+
if (dialect != null && !dialect.getIri().equals(validationContext.getMetaSchema().getIri())) {
389391
SchemaValidatorsConfig config = validationContext.getConfig();
390-
if (metaSchema.getKeywords().containsKey("discriminator") && !config.isDiscriminatorKeywordEnabled()) {
392+
if (dialect.getKeywords().containsKey("discriminator") && !config.isDiscriminatorKeywordEnabled()) {
391393
config = SchemaValidatorsConfig.builder(config)
392394
.discriminatorKeywordEnabled(true)
393395
.nullableKeywordEnabled(true)
394396
.build();
395397
}
396-
return new ValidationContext(metaSchema, validationContext.getJsonSchemaFactory(), config,
398+
return new ValidationContext(dialect, validationContext.getJsonSchemaFactory(), config,
397399
validationContext.getSchemaReferences(), validationContext.getSchemaResources(),
398400
validationContext.getDynamicAnchors());
399401
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.networknt.schema;
1717

18+
import com.networknt.schema.dialect.Dialect;
19+
1820
/**
1921
* Json schema version.
2022
*/

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import java.util.Optional;
1919

20+
import com.networknt.schema.dialect.DialectId;
21+
2022
/**
2123
* The JSON Schema specification which defines the standard dialects.
2224
*/

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,32 @@
2222

2323
import com.fasterxml.jackson.databind.JsonNode;
2424
import com.networknt.schema.Specification.Version;
25+
import com.networknt.schema.dialect.Dialect;
2526
import com.networknt.schema.keyword.KeywordValidator;
2627

2728
public class ValidationContext {
28-
private final Dialect metaSchema;
29+
private final Dialect dialect;
2930
private final JsonSchemaFactory jsonSchemaFactory;
3031
private final SchemaValidatorsConfig config;
3132
private final ConcurrentMap<String, JsonSchema> schemaReferences;
3233
private final ConcurrentMap<String, JsonSchema> schemaResources;
3334
private final ConcurrentMap<String, JsonSchema> dynamicAnchors;
3435

35-
public ValidationContext(Dialect metaSchema,
36+
public ValidationContext(Dialect dialect,
3637
JsonSchemaFactory jsonSchemaFactory, SchemaValidatorsConfig config) {
37-
this(metaSchema, jsonSchemaFactory, config, new ConcurrentHashMap<>(), new ConcurrentHashMap<>(), new ConcurrentHashMap<>());
38+
this(dialect, jsonSchemaFactory, config, new ConcurrentHashMap<>(), new ConcurrentHashMap<>(), new ConcurrentHashMap<>());
3839
}
3940

40-
public ValidationContext(Dialect metaSchema, JsonSchemaFactory jsonSchemaFactory,
41+
public ValidationContext(Dialect dialect, JsonSchemaFactory jsonSchemaFactory,
4142
SchemaValidatorsConfig config, ConcurrentMap<String, JsonSchema> schemaReferences,
4243
ConcurrentMap<String, JsonSchema> schemaResources, ConcurrentMap<String, JsonSchema> dynamicAnchors) {
43-
if (metaSchema == null) {
44+
if (dialect == null) {
4445
throw new IllegalArgumentException("JsonMetaSchema must not be null");
4546
}
4647
if (jsonSchemaFactory == null) {
4748
throw new IllegalArgumentException("JsonSchemaFactory must not be null");
4849
}
49-
this.metaSchema = metaSchema;
50+
this.dialect = dialect;
5051
this.jsonSchemaFactory = jsonSchemaFactory;
5152
// The deprecated SchemaValidatorsConfig constructor needs to remain until removed
5253
this.config = config == null ? new SchemaValidatorsConfig() : config;
@@ -61,11 +62,11 @@ public JsonSchema newSchema(SchemaLocation schemaLocation, JsonNodePath evaluati
6162

6263
public KeywordValidator newValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath,
6364
String keyword /* keyword */, JsonNode schemaNode, JsonSchema parentSchema) {
64-
return this.metaSchema.newValidator(this, schemaLocation, evaluationPath, keyword, schemaNode, parentSchema);
65+
return this.dialect.newValidator(this, schemaLocation, evaluationPath, keyword, schemaNode, parentSchema);
6566
}
6667

6768
public String resolveSchemaId(JsonNode schemaNode) {
68-
return this.metaSchema.readId(schemaNode);
69+
return this.dialect.readId(schemaNode);
6970
}
7071

7172
public JsonSchemaFactory getJsonSchemaFactory() {
@@ -104,10 +105,10 @@ public ConcurrentMap<String, JsonSchema> getDynamicAnchors() {
104105
}
105106

106107
public Dialect getMetaSchema() {
107-
return this.metaSchema;
108+
return this.dialect;
108109
}
109110

110111
public Optional<Version> activeDialect() {
111-
return Optional.of(this.metaSchema.getSpecification());
112+
return Optional.of(this.dialect.getSpecification());
112113
}
113114
}

src/main/java/com/networknt/schema/Dialect.java renamed to src/main/java/com/networknt/schema/dialect/Dialect.java

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,21 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.networknt.schema;
17+
package com.networknt.schema.dialect;
1818

1919
import com.fasterxml.jackson.databind.JsonNode;
20+
import com.networknt.schema.Error;
21+
import com.networknt.schema.Format;
22+
import com.networknt.schema.InvalidSchemaException;
23+
import com.networknt.schema.JsonNodePath;
24+
import com.networknt.schema.JsonSchema;
25+
import com.networknt.schema.JsonSchemaException;
26+
import com.networknt.schema.SchemaLocation;
27+
import com.networknt.schema.Specification;
28+
import com.networknt.schema.ValidationContext;
29+
import com.networknt.schema.Vocabularies;
30+
import com.networknt.schema.Vocabulary;
31+
import com.networknt.schema.VocabularyFactory;
2032
import com.networknt.schema.Specification.Version;
2133
import com.networknt.schema.keyword.FormatKeyword;
2234
import com.networknt.schema.keyword.Keyword;
@@ -303,26 +315,6 @@ public Dialect build() {
303315
Map<String, Keyword> result = createKeywordsMap(keywords, this.formats);
304316
return new Dialect(this.iri, this.idKeyword, result, this.vocabularies, this.specification, this);
305317
}
306-
307-
@Deprecated
308-
public Builder addKeyword(Keyword keyword) {
309-
return keyword(keyword);
310-
}
311-
312-
@Deprecated
313-
public Builder addKeywords(Collection<? extends Keyword> keywords) {
314-
return keywords(keywords);
315-
}
316-
317-
@Deprecated
318-
public Builder addFormat(Format format) {
319-
return format(format);
320-
}
321-
322-
@Deprecated
323-
public Builder addFormats(Collection<? extends Format> formats) {
324-
return formats(formats);
325-
}
326318
}
327319

328320
private final String iri;
@@ -352,34 +344,8 @@ public Builder addFormats(Collection<? extends Format> formats) {
352344
this.builder = builder;
353345
}
354346

355-
public static Dialect getV4() {
356-
return new Version4().getInstance();
357-
}
358-
359-
public static Dialect getV6() {
360-
return new Version6().getInstance();
361-
}
362-
363-
public static Dialect getV7() {
364-
return new Version7().getInstance();
365-
}
366-
367-
public static Dialect getV201909() {
368-
return new Version201909().getInstance();
369-
}
370-
371-
public static Dialect getV202012() {
372-
return new Version202012().getInstance();
373-
}
374-
375347
/**
376348
* Create a builder without keywords or formats.
377-
* <p>
378-
* Use {@link #getV4()} for the Draft 4 Metaschema, or if you need a builder based on Draft4, use
379-
*
380-
* <code>
381-
* JsonMetaSchema.builder("http://your-metaschema-iri", JsonMetaSchema.getV4()).build();
382-
* </code>
383349
*
384350
* @param iri the IRI of the metaschema that will be defined via this builder.
385351
* @return a builder instance without any keywords or formats - usually not what one needs.

src/main/java/com/networknt/schema/DialectId.java renamed to src/main/java/com/networknt/schema/dialect/DialectId.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.networknt.schema;
16+
package com.networknt.schema.dialect;
1717

1818
/**
1919
* The dialect id is an IRI that points to the meta-schema that can be used to
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 2025 the original author or authors.
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+
17+
package com.networknt.schema.dialect;
18+
19+
/**
20+
* The dialects.
21+
*/
22+
public class Dialects {
23+
/**
24+
* Draft 4.
25+
*
26+
* @return the Draft 4 dialect
27+
*/
28+
public static Dialect getDraft4() {
29+
return Draft4.getInstance();
30+
}
31+
32+
/**
33+
* Draft 6.
34+
*
35+
* @return the Draft 6 dialect
36+
*/
37+
public static Dialect getDraft6() {
38+
return Draft6.getInstance();
39+
}
40+
41+
/**
42+
* Draft 7.
43+
*
44+
* @return the Draft 7 dialect
45+
*/
46+
public static Dialect getDraft7() {
47+
return Draft7.getInstance();
48+
}
49+
50+
/**
51+
* Draft 2019-09.
52+
*
53+
* @return the Draft 2019-09 dialect
54+
*/
55+
public static Dialect getDraft201909() {
56+
return Draft201909.getInstance();
57+
}
58+
59+
/**
60+
* Draft 2020-12.
61+
*
62+
* @return the Draft 2020-12 dialect
63+
*/
64+
public static Dialect getDraft202012() {
65+
return Draft202012.getInstance();
66+
}
67+
68+
/**
69+
* OpenAPI 3.0.
70+
*
71+
* @return the OpenAPI 3.0 dialect
72+
*/
73+
public static Dialect getOpenApi30() {
74+
return OpenApi30.getInstance();
75+
}
76+
77+
/**
78+
* OpenAPI 3.1.
79+
*
80+
* @return the OpenAPI 3.1 dialect
81+
*/
82+
public static Dialect getOpenApi31() {
83+
return OpenApi31.getInstance();
84+
}
85+
}

0 commit comments

Comments
 (0)