Skip to content

Commit 71f44b3

Browse files
committed
test for keeping format names of custom formats
1 parent 9ce906a commit 71f44b3

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,19 @@ public static class SchemaLoaderBuilder {
7777

7878
public SchemaLoaderBuilder addFormatValidator(final String formatName,
7979
final FormatValidator formatValidator) {
80-
formatValidators.put(formatName, formatValidator);
80+
FormatValidator wrappingFormatValidator = new FormatValidator() {
81+
82+
@Override
83+
public Optional<String> validate(String subject) {
84+
return formatValidator.validate(subject);
85+
}
86+
87+
@Override
88+
public String formatName() {
89+
return formatName;
90+
}
91+
};
92+
formatValidators.put(formatName, wrappingFormatValidator);
8193
return this;
8294
}
8395

core/src/test/java/org/everit/json/schema/loader/CustomFormatValidatorTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
import java.util.Optional;
2727

28+
import static org.junit.Assert.assertEquals;
29+
2830
public class CustomFormatValidatorTest {
2931

3032
private final ResourceLoader loader = ResourceLoader.DEFAULT;
@@ -39,6 +41,11 @@ public Optional<String> validate(final String subject) {
3941
return Optional.of(String.format("the length of srtring [%s] is odd", subject));
4042
}
4143
}
44+
45+
@Override
46+
public String formatName() {
47+
return "evenlength";
48+
}
4249
}
4350

4451
@Test
@@ -52,7 +59,20 @@ public void test() {
5259
Assert.fail("did not throw exception");
5360
} catch (ValidationException ve) {
5461
}
62+
}
5563

64+
@Test
65+
public void nameOverride() {
66+
JSONObject rawSchemaJson = loader.readObj("customformat-schema.json");
67+
JSONObject idPropSchema = (JSONObject) rawSchemaJson.query("/properties/id");
68+
idPropSchema.put("format", "somethingelse");
69+
SchemaLoader schemaLoader = SchemaLoader.builder()
70+
.schemaJson(rawSchemaJson)
71+
.addFormatValidator("somethingelse", new EvenCharNumValidator())
72+
.build();
73+
Object actual = new JSONObject(schemaLoader.load().build().toString())
74+
.query("/properties/id/format");
75+
assertEquals("somethingelse", actual);
5676
}
5777

5878
}

0 commit comments

Comments
 (0)