Skip to content

Commit fe2e318

Browse files
committed
Refactor SchemaRegistry and ResourceLoaders
1 parent 9e8dfe1 commit fe2e318

File tree

3 files changed

+83
-40
lines changed

3 files changed

+83
-40
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@
3636
import java.io.IOException;
3737
import java.io.InputStream;
3838
import java.net.URI;
39+
import java.util.Map;
3940
import java.util.concurrent.ConcurrentHashMap;
4041
import java.util.concurrent.ConcurrentMap;
4142
import java.util.function.Consumer;
43+
import java.util.function.Function;
4244

4345
/**
4446
* Registry for loading and registering {@link Schema} instances.
@@ -158,6 +160,43 @@ public Builder schemaRegistryConfig(SchemaRegistryConfig schemaRegistryConfig) {
158160
return this;
159161
}
160162

163+
/**
164+
* Sets the schema data by absolute IRI.
165+
*
166+
* @param schemas the map of IRI to schema data
167+
* @return the builder
168+
*/
169+
public Builder schemas(Map<String, String> schemas) {
170+
return this.resourceLoaders(resourceLoaders -> resourceLoaders.resources(schemas));
171+
}
172+
173+
/**
174+
* Sets the schema data by absolute IRI function.
175+
*
176+
* @param schemas the function that returns schema data given IRI
177+
* @return the builder
178+
*/
179+
public Builder schemas(Function<String, String> schemas) {
180+
return this.resourceLoaders(resourceLoaders -> resourceLoaders.resources(schemas));
181+
}
182+
183+
/**
184+
* Sets the schema data by using two mapping functions.
185+
* <p>
186+
* Firstly to map the IRI to an object. If the object is null no mapping is
187+
* performed.
188+
* <p>
189+
* Next to map the object to the schema data.
190+
*
191+
* @param <T> the type of the object
192+
* @param mapIriToObject the mapping of IRI to object
193+
* @param mapObjectToData the mappingof object to schema data
194+
* @return the builder
195+
*/
196+
public <T> Builder schemas(Function<String, T> mapIriToObject, Function<T, String> mapObjectToData) {
197+
return this.resourceLoaders(resourceLoaders -> resourceLoaders.resources(mapIriToObject, mapObjectToData));
198+
}
199+
161200
public SchemaRegistry build() {
162201
return new SchemaRegistry(nodeReader, defaultDialectId, schemaLoader, enableSchemaCache,
163202
dialectRegistry, schemaRegistryConfig);

src/main/java/com/networknt/schema/resource/ResourceLoaders.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.util.function.Function;
2424

2525
/**
26-
* Schema Loaders used to load a resource given the retrieval IRI.
26+
* Resource Loaders used to load a resource given the retrieval IRI.
2727
*/
2828
public class ResourceLoaders extends ArrayList<ResourceLoader> {
2929
private static final long serialVersionUID = 1L;
@@ -62,7 +62,7 @@ public Builder with(Builder builder) {
6262
}
6363

6464
/**
65-
* Customize the schema loaders.
65+
* Customize the resource loaders.
6666
*
6767
* @param customizer the customizer
6868
* @return the builder
@@ -73,20 +73,20 @@ public Builder values(Consumer<List<ResourceLoader>> customizer) {
7373
}
7474

7575
/**
76-
* Adds a schema loader.
76+
* Adds a resource loader.
7777
*
78-
* @param schemaLoader the schema loader
78+
* @param resourceLoader the resource loader
7979
* @return the builder
8080
*/
81-
public Builder add(ResourceLoader schemaLoader) {
82-
this.values.add(schemaLoader);
81+
public Builder add(ResourceLoader resourceLoader) {
82+
this.values.add(resourceLoader);
8383
return this;
8484
}
8585

8686
/**
87-
* Sets the schema data by absolute IRI.
87+
* Sets the resource data by absolute IRI.
8888
*
89-
* @param resources the map of IRI to schema data
89+
* @param resources the map of IRI to resource data
9090
* @return the builder
9191
*/
9292
public Builder resources(Map<String, String> resources) {
@@ -95,18 +95,18 @@ public Builder resources(Map<String, String> resources) {
9595
}
9696

9797
/**
98-
* Sets the schema data by absolute IRI function.
98+
* Sets the resource data by absolute IRI function.
9999
*
100-
* @param schemas the function that returns schema data given IRI
100+
* @param resources the function that returns resource data given IRI
101101
* @return the builder
102102
*/
103-
public Builder resources(Function<String, String> schemas) {
104-
this.values.add(new MapResourceLoader(schemas));
103+
public Builder resources(Function<String, String> resources) {
104+
this.values.add(new MapResourceLoader(resources));
105105
return this;
106106
}
107107

108108
/**
109-
* Sets the schema data by using two mapping functions.
109+
* Sets the resource data by using two mapping functions.
110110
* <p>
111111
* Firstly to map the IRI to an object. If the object is null no mapping is
112112
* performed.
@@ -118,15 +118,15 @@ public Builder resources(Function<String, String> schemas) {
118118
* @param mapObjectToData the mappingof object to schema data
119119
* @return the builder
120120
*/
121-
public <T> Builder schemas(Function<String, T> mapIriToObject, Function<T, String> mapObjectToData) {
121+
public <T> Builder resources(Function<String, T> mapIriToObject, Function<T, String> mapObjectToData) {
122122
this.values.add(new MapResourceLoader(mapIriToObject, mapObjectToData));
123123
return this;
124124
}
125125

126126
/**
127127
* Builds a {@link ResourceLoaders}.
128128
*
129-
* @return the schema loaders
129+
* @return the resource loaders
130130
*/
131131
public ResourceLoaders build() {
132132
return values;

src/test/java/com/networknt/schema/SampleTest.java renamed to src/test/java/com/networknt/schema/QuickStartTest.java

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@
1212
import com.networknt.schema.serialization.JsonMapperFactory;
1313

1414
/**
15-
* Sample test.
15+
* Quick start test.
1616
*/
17-
class SampleTest {
17+
class QuickStartTest {
1818
@Test
1919
void schemaFromSchemaLocationMapping() {
20-
SchemaRegistry factory = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12, builder -> builder.schemaIdResolvers(
21-
schemaIdResolvers -> schemaIdResolvers.mapPrefix("https://www.example.com/schema", "classpath:schema")));
20+
SchemaRegistry schemaRegistry = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12,
21+
builder -> builder.schemaIdResolvers(schemaIdResolvers -> schemaIdResolvers
22+
.mapPrefix("https://www.example.com/schema", "classpath:schema")));
2223
/*
2324
* This should be cached for performance.
2425
*/
25-
Schema schemaFromSchemaLocation = factory
26+
Schema schemaFromSchemaLocation = schemaRegistry
2627
.getSchema(SchemaLocation.of("https://www.example.com/schema/example-ref.json"));
2728
/*
2829
* By default all schemas are preloaded eagerly but ref resolve failures are not
@@ -31,21 +32,22 @@ void schemaFromSchemaLocationMapping() {
3132
*/
3233
schemaFromSchemaLocation.initializeValidators();
3334
List<Error> errors = schemaFromSchemaLocation.validate("{\"id\": \"2\"}", InputFormat.JSON,
34-
executionContext -> executionContext.executionConfig(executionConfig -> executionConfig.formatAssertionsEnabled(true)));
35+
executionContext -> executionContext
36+
.executionConfig(executionConfig -> executionConfig.formatAssertionsEnabled(true)));
3537
assertEquals(1, errors.size());
3638
}
3739

3840
@Test
3941
void schemaFromSchemaLocationContent() {
4042
String schemaData = "{\"enum\":[1, 2, 3, 4]}";
41-
42-
SchemaRegistry factory = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12,
43-
builder -> builder.resourceLoaders(resourceLoaders -> resourceLoaders.resources(
44-
Collections.singletonMap("https://www.example.com/schema/example-ref.json", schemaData))));
43+
44+
SchemaRegistry schemaRegistry = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12,
45+
builder -> builder.schemas(
46+
Collections.singletonMap("https://www.example.com/schema/example-ref.json", schemaData)));
4547
/*
4648
* This should be cached for performance.
4749
*/
48-
Schema schemaFromSchemaLocation = factory
50+
Schema schemaFromSchemaLocation = schemaRegistry
4951
.getSchema(SchemaLocation.of("https://www.example.com/schema/example-ref.json"));
5052
/*
5153
* By default all schemas are preloaded eagerly but ref resolve failures are not
@@ -54,50 +56,51 @@ void schemaFromSchemaLocationContent() {
5456
*/
5557
schemaFromSchemaLocation.initializeValidators();
5658
List<Error> errors = schemaFromSchemaLocation.validate("{\"id\": \"2\"}", InputFormat.JSON,
57-
executionContext -> executionContext.executionConfig(executionConfig -> executionConfig.formatAssertionsEnabled(true)));
59+
executionContext -> executionContext
60+
.executionConfig(executionConfig -> executionConfig.formatAssertionsEnabled(true)));
5861
assertEquals(1, errors.size());
5962
}
6063

6164
@Test
6265
void schemaFromClasspath() {
63-
SchemaRegistry factory = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12);
66+
SchemaRegistry schemaRegistry = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12);
6467
/*
6568
* This should be cached for performance.
6669
*
6770
* Loading from using the retrieval IRI is not recommended as it may cause
6871
* confusing when resolving relative $ref when $id is also used.
6972
*/
70-
Schema schemaFromClasspath = factory.getSchema(SchemaLocation.of("classpath:schema/example-ref.json"));
73+
Schema schemaFromClasspath = schemaRegistry.getSchema(SchemaLocation.of("classpath:schema/example-ref.json"));
7174
/*
7275
* By default all schemas are preloaded eagerly but ref resolve failures are not
7376
* thrown. You check if there are issues with ref resolving using
7477
* initializeValidators()
7578
*/
7679
schemaFromClasspath.initializeValidators();
7780
List<Error> errors = schemaFromClasspath.validate("{\"id\": \"2\"}", InputFormat.JSON,
78-
executionContext -> executionContext.executionConfig(executionConfig -> executionConfig.formatAssertionsEnabled(true)));
81+
executionContext -> executionContext
82+
.executionConfig(executionConfig -> executionConfig.formatAssertionsEnabled(true)));
7983
assertEquals(1, errors.size());
8084
}
8185

8286
@Test
8387
void schemaFromString() {
84-
SchemaRegistry factory = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12);
88+
SchemaRegistry schemaRegistry = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12);
8589
/*
8690
* This should be cached for performance.
8791
*
8892
* Loading from a String is not recommended as there is no base IRI to use for
8993
* resolving relative $ref.
9094
*/
91-
Schema schemaFromString = factory
92-
.getSchema("{\"enum\":[1, 2, 3, 4]}");
93-
List<Error> errors = schemaFromString.validate("7", InputFormat.JSON,
94-
executionContext -> executionContext.executionConfig(executionConfig -> executionConfig.formatAssertionsEnabled(true)));
95+
Schema schemaFromString = schemaRegistry.getSchema("{\"enum\":[1, 2, 3, 4]}");
96+
List<Error> errors = schemaFromString.validate("7", InputFormat.JSON, executionContext -> executionContext
97+
.executionConfig(executionConfig -> executionConfig.formatAssertionsEnabled(true)));
9598
assertEquals(1, errors.size());
9699
}
97100

98101
@Test
99102
void schemaFromJsonNode() throws JsonProcessingException {
100-
SchemaRegistry factory = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12);
103+
SchemaRegistry schemaRegistry = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12);
101104
JsonNode schemaNode = JsonMapperFactory.getInstance().readTree(
102105
"{\"$schema\": \"http://json-schema.org/draft-06/schema#\", \"properties\": { \"id\": {\"type\": \"number\"}}}");
103106
/*
@@ -106,18 +109,19 @@ void schemaFromJsonNode() throws JsonProcessingException {
106109
* Loading from a JsonNode is not recommended as there is no base IRI to use for
107110
* resolving relative $ref.
108111
*
109-
* Note that the V202012 from the factory is the default version if $schema is not
110-
* specified. As $schema is specified in the data, V6 is used.
112+
* Note that the V202012 from the schemaRegistry is the default version if $schema is
113+
* not specified. As $schema is specified in the data, V6 is used.
111114
*/
112-
Schema schemaFromNode = factory.getSchema(schemaNode);
115+
Schema schemaFromNode = schemaRegistry.getSchema(schemaNode);
113116
/*
114117
* By default all schemas are preloaded eagerly but ref resolve failures are not
115118
* thrown. You check if there are issues with ref resolving using
116119
* initializeValidators()
117120
*/
118121
schemaFromNode.initializeValidators();
119122
List<Error> errors = schemaFromNode.validate("{\"id\": \"2\"}", InputFormat.JSON,
120-
executionContext -> executionContext.executionConfig(executionConfig -> executionConfig.formatAssertionsEnabled(true)));
123+
executionContext -> executionContext
124+
.executionConfig(executionConfig -> executionConfig.formatAssertionsEnabled(true)));
121125
assertEquals(1, errors.size());
122126
}
123127
}

0 commit comments

Comments
 (0)