15
15
*/
16
16
package org .everit .json .schema ;
17
17
18
+ import java .util .ArrayList ;
19
+ import java .util .Arrays ;
20
+ import java .util .Collections ;
21
+ import java .util .List ;
22
+ import java .util .Objects ;
18
23
import java .util .regex .Pattern ;
19
24
20
25
/**
@@ -35,11 +40,24 @@ public static class Builder extends Schema.Builder<StringSchema> {
35
40
36
41
private boolean requiresString = true ;
37
42
43
+ private FormatValidator formatValidator = FormatValidator .NONE ;
44
+
38
45
@ Override
39
46
public StringSchema build () {
40
47
return new StringSchema (this );
41
48
}
42
49
50
+ /**
51
+ * Setter for the format validator. It should be used in conjunction with
52
+ * {@link FormatValidator#forFormat(String)} if a {@code "format"} value is found in a schema
53
+ * json.
54
+ */
55
+ public Builder formatValidator (final FormatValidator formatValidator ) {
56
+ this .formatValidator = Objects .requireNonNull (formatValidator ,
57
+ "formatValidator cannot be null" );
58
+ return this ;
59
+ }
60
+
43
61
public Builder maxLength (final Integer maxLength ) {
44
62
this .maxLength = maxLength ;
45
63
return this ;
@@ -74,6 +92,8 @@ public static Builder builder() {
74
92
75
93
private final boolean requiresString ;
76
94
95
+ private final FormatValidator formatValidator ;
96
+
77
97
public StringSchema () {
78
98
this (builder ());
79
99
}
@@ -94,6 +114,7 @@ public StringSchema(final Builder builder) {
94
114
} else {
95
115
this .pattern = null ;
96
116
}
117
+ this .formatValidator = builder .formatValidator ;
97
118
}
98
119
99
120
public Integer getMaxLength () {
@@ -108,23 +129,27 @@ public Pattern getPattern() {
108
129
return pattern ;
109
130
}
110
131
111
- private void testLength (final String subject ) {
132
+ private List < ValidationException > testLength (final String subject ) {
112
133
int actualLength = subject .length ();
134
+ List <ValidationException > rval = new ArrayList <>();
113
135
if (minLength != null && actualLength < minLength .intValue ()) {
114
- throw new ValidationException (this , "expected minLength: " + minLength + ", actual: "
115
- + actualLength );
136
+ rval . add ( new ValidationException (this , "expected minLength: " + minLength + ", actual: "
137
+ + actualLength )) ;
116
138
}
117
139
if (maxLength != null && actualLength > maxLength .intValue ()) {
118
- throw new ValidationException (this , "expected maxLength: " + maxLength + ", actual: "
119
- + actualLength );
140
+ rval . add ( new ValidationException (this , "expected maxLength: " + maxLength + ", actual: "
141
+ + actualLength )) ;
120
142
}
143
+ return rval ;
121
144
}
122
145
123
- private void testPattern (final String subject ) {
146
+ private List < ValidationException > testPattern (final String subject ) {
124
147
if (pattern != null && !pattern .matcher (subject ).find ()) {
125
- throw new ValidationException (this , String .format ("string [%s] does not match pattern %s" ,
126
- subject , pattern .pattern ()));
148
+ return Arrays .asList (new ValidationException (this , String .format (
149
+ "string [%s] does not match pattern %s" ,
150
+ subject , pattern .pattern ())));
127
151
}
152
+ return Collections .emptyList ();
128
153
}
129
154
130
155
@ Override
@@ -135,9 +160,13 @@ public void validate(final Object subject) {
135
160
}
136
161
} else {
137
162
String stringSubject = (String ) subject ;
138
- testLength (stringSubject );
139
- testPattern (stringSubject );
163
+ List <ValidationException > rval = new ArrayList <>();
164
+ rval .addAll (testLength (stringSubject ));
165
+ rval .addAll (testPattern (stringSubject ));
166
+ formatValidator .validate (stringSubject )
167
+ .map (failure -> new ValidationException (this , failure ))
168
+ .ifPresent (rval ::add );
169
+ ValidationException .throwFor (this , rval );
140
170
}
141
171
}
142
-
143
172
}
0 commit comments