File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed
main/java/org/everit/json/schema/loader
test/java/org/everit/json/schema/loader Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -77,7 +77,19 @@ public static class SchemaLoaderBuilder {
77
77
78
78
public SchemaLoaderBuilder addFormatValidator (final String formatName ,
79
79
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 );
81
93
return this ;
82
94
}
83
95
Original file line number Diff line number Diff line change 25
25
26
26
import java .util .Optional ;
27
27
28
+ import static org .junit .Assert .assertEquals ;
29
+
28
30
public class CustomFormatValidatorTest {
29
31
30
32
private final ResourceLoader loader = ResourceLoader .DEFAULT ;
@@ -39,6 +41,11 @@ public Optional<String> validate(final String subject) {
39
41
return Optional .of (String .format ("the length of srtring [%s] is odd" , subject ));
40
42
}
41
43
}
44
+
45
+ @ Override
46
+ public String formatName () {
47
+ return "evenlength" ;
48
+ }
42
49
}
43
50
44
51
@ Test
@@ -52,7 +59,20 @@ public void test() {
52
59
Assert .fail ("did not throw exception" );
53
60
} catch (ValidationException ve ) {
54
61
}
62
+ }
55
63
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 );
56
76
}
57
77
58
78
}
You can’t perform that action at this time.
0 commit comments