Skip to content

Commit df06811

Browse files
committed
fixing #152
* mostly taking the code from #153 * also adding testing infrastructure for using custom format validators * (unrelated) properly overriding `FormatValidator#formatName()` in `RegexFormatValidator`
1 parent 16748b6 commit df06811

File tree

9 files changed

+89
-9
lines changed

9 files changed

+89
-9
lines changed

core/src/main/java/org/everit/json/schema/internal/RegexFormatValidator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@ public class RegexFormatValidator implements FormatValidator {
1717
return Optional.empty();
1818
}
1919

20+
@Override public String formatName() {
21+
return "regex";
22+
}
2023
}

core/src/main/java/org/everit/json/schema/loader/LoadingState.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.Collection;
1212
import java.util.Collections;
1313
import java.util.Comparator;
14+
import java.util.HashMap;
1415
import java.util.List;
1516
import java.util.Map;
1617

@@ -74,7 +75,7 @@ static URI extractChildId(URI parentScopeId, Object childJson, String idKeyword)
7475
SchemaLoader.SchemaLoaderBuilder initChildLoader() {
7576
SchemaLoader.SchemaLoaderBuilder rval = SchemaLoader.builder()
7677
.httpClient(this.config.httpClient)
77-
.formatValidators(this.config.formatValidators)
78+
.formatValidators(new HashMap<>(this.config.formatValidators))
7879
.resolutionScope(id)
7980
.schemaJson(schemaJson)
8081
.rootSchemaJson(rootSchemaJson)

core/src/main/java/org/everit/json/schema/loader/SchemaLoader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static class SchemaLoaderBuilder {
6767

6868
List<String> pointerToCurrentObj = emptyList();
6969

70-
Map<String, FormatValidator> formatValidators;
70+
Map<String, FormatValidator> formatValidators = new HashMap<>();
7171

7272
SpecificationVersion specVersion;
7373

@@ -122,7 +122,8 @@ public SchemaLoaderBuilder draftV7Support() {
122122

123123
private void setSpecVersion(SpecificationVersion specVersion) {
124124
this.specVersion = specVersion;
125-
this.formatValidators = new HashMap<>(specVersion.defaultFormatValidators());
125+
specVersion.defaultFormatValidators().forEach(this::addFormatValidator);
126+
// this.formatValidators = new HashMap<>(specVersion.defaultFormatValidators());
126127
}
127128

128129
public SchemaLoader build() {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.everit.json.schema;
2+
3+
import java.util.Optional;
4+
5+
public class EvenCharNumValidator implements FormatValidator {
6+
7+
@Override
8+
public Optional<String> validate(final String subject) {
9+
if (subject.length() % 2 == 0) {
10+
return Optional.empty();
11+
} else {
12+
return Optional.of(String.format("the length of srtring [%s] is odd", subject));
13+
}
14+
}
15+
16+
@Override
17+
public String formatName() {
18+
return "evenlength";
19+
}
20+
}

tests/src/test/java/org/everit/json/schema/IssueTest.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
import java.io.FileNotFoundException;
88
import java.io.IOException;
99
import java.io.UncheckedIOException;
10+
import java.lang.reflect.Constructor;
1011
import java.net.URISyntaxException;
1112
import java.util.ArrayList;
1213
import java.util.Arrays;
1314
import java.util.List;
15+
import java.util.Map;
1416
import java.util.Optional;
1517

1618
import org.everit.json.schema.loader.SchemaLoader;
@@ -82,14 +84,37 @@ private Schema loadSchema() {
8284
try {
8385
if (schemaFile.isPresent()) {
8486
JSONObject schemaObj = fileAsJson(schemaFile.get());
85-
return SchemaLoader.load(schemaObj);
87+
SchemaLoader.SchemaLoaderBuilder loaderBuilder = SchemaLoader.builder().schemaJson(schemaObj);
88+
setupCustomFormatValidators(loaderBuilder);
89+
return loaderBuilder.build().load().build();
8690
}
8791
throw new RuntimeException(issueDir.getCanonicalPath() + "/schema.json is not found");
8892
} catch (IOException e) {
8993
throw new UncheckedIOException(e);
9094
}
9195
}
9296

97+
private void setupCustomFormatValidators(SchemaLoader.SchemaLoaderBuilder loaderBuilder) {
98+
fileByName("validator-config.json").map(IssueTest::fileAsJson)
99+
.filter(json -> json.has("customFormats"))
100+
.map(json -> json.getJSONObject("customFormats"))
101+
.ifPresent(customFormatsJson -> customFormatsJson.toMap()
102+
.entrySet()
103+
.forEach(entry -> loaderBuilder
104+
.addFormatValidator(entry.getKey(), this.createFormatValidator(entry))));
105+
}
106+
107+
private FormatValidator createFormatValidator(Map.Entry<String, Object> entry) {
108+
String formatClassName = (String) entry.getValue();
109+
try {
110+
Class<? extends FormatValidator> formatClass = (Class<? extends FormatValidator>) Class.forName(formatClassName);
111+
Constructor<? extends FormatValidator> ctor = formatClass.getConstructor();
112+
return ctor.newInstance();
113+
} catch (Exception e) {
114+
throw new RuntimeException(e);
115+
}
116+
}
117+
93118
private void stopJetty() {
94119
if (servletSupport != null) {
95120
servletSupport.stop();
@@ -100,16 +125,19 @@ private void stopJetty() {
100125
public void test() {
101126
Assume.assumeFalse("issue dir starts with 'x' - ignoring", issueDir.getName().startsWith("x"));
102127
fileByName("remotes").ifPresent(this::initJetty);
103-
Schema schema = loadSchema();
104-
fileByName("subject-valid.json").ifPresent(file -> validate(file, schema, true));
105-
fileByName("subject-invalid.json").ifPresent(file -> validate(file, schema, false));
106-
stopJetty();
128+
try {
129+
Schema schema = loadSchema();
130+
fileByName("subject-valid.json").ifPresent(file -> validate(file, schema, true));
131+
fileByName("subject-invalid.json").ifPresent(file -> validate(file, schema, false));
132+
} finally {
133+
stopJetty();
134+
}
107135
}
108136

109137
private Validator createValidator() {
110138
Validator.ValidatorBuilder builder = Validator.builder();
111139
fileByName("validator-config.json").map(file -> fileAsJson(file))
112-
.map(json -> json.getBoolean("failEarly"))
140+
.map(json -> json.optBoolean("failEarly"))
113141
.filter(bool -> Boolean.TRUE.equals(bool))
114142
.ifPresent(t -> builder.failEarly());
115143
return builder.build();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-06/schema#",
3+
"properties": {
4+
"id": {
5+
"format": "evenlength",
6+
"maxLength": 10,
7+
"minLength": 3,
8+
"type": "string"
9+
}
10+
},
11+
"required": [
12+
"id"
13+
],
14+
"type": "object"
15+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$ref": "http://localhost:1234/customformat-schema.json",
3+
"$schema": "http://json-schema.org/draft-06/schema#"
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"id": "everitorg"
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"customFormats": {
3+
"evenlength": "org.everit.json.schema.EvenCharNumValidator"
4+
}
5+
}

0 commit comments

Comments
 (0)