@@ -126,7 +126,7 @@ public OneOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
126
126
}
127
127
128
128
public Set <ValidationMessage > validate (JsonNode node , JsonNode rootNode , String at ) {
129
- Set <ValidationMessage > errors = new LinkedHashSet <ValidationMessage >();
129
+ Set <ValidationMessage > errors = new LinkedHashSet <>();
130
130
131
131
// As oneOf might contain multiple schemas take a backup of evaluatedProperties.
132
132
Object backupEvaluatedProperties = CollectorContext .getInstance ().get (UnEvaluatedPropertiesValidator .EVALUATED_PROPERTIES );
@@ -143,14 +143,13 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
143
143
state .setComplexValidator (true );
144
144
145
145
int numberOfValidSchema = 0 ;
146
- Set <ValidationMessage > childErrors = new LinkedHashSet <ValidationMessage >();
146
+ Set <ValidationMessage > childErrors = new LinkedHashSet <>();
147
147
// validate that only a single element has been received in the oneOf node
148
148
// validation should not continue, as it contradicts the oneOf requirement of only one
149
149
// if(node.isObject() && node.size()>1) {
150
150
// errors = Collections.singleton(buildValidationMessage(at, ""));
151
151
// return Collections.unmodifiableSet(errors);
152
152
// }
153
-
154
153
for (ShortcutValidator validator : schemas ) {
155
154
Set <ValidationMessage > schemaErrors = null ;
156
155
// Reset state in case the previous validator did not match
@@ -191,36 +190,15 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
191
190
Set <ValidationMessage > childNotRequiredErrors = childErrors .stream ().filter (error -> !ValidatorTypeCode .REQUIRED .getValue ().equals (error .getType ())).collect (Collectors .toSet ());
192
191
193
192
// ensure there is always an "OneOf" error reported if number of valid schemas is not equal to 1.
194
- if (numberOfValidSchema > 1 ) {
193
+ // TODO We need to break it into two errors in the future.
194
+ if (numberOfValidSchema != 1 ) {
195
195
final ValidationMessage message = getMultiSchemasValidErrorMsg (at );
196
196
if (failFast ) {
197
197
throw new JsonSchemaException (message );
198
198
}
199
199
errors .add (message );
200
200
}
201
201
202
- // ensure there is always an "OneOf" error reported if number of valid schemas is not equal to 1.
203
- else if (numberOfValidSchema < 1 ) {
204
- if (!childNotRequiredErrors .isEmpty ()) {
205
- childErrors = childNotRequiredErrors ;
206
- }
207
- if (!childErrors .isEmpty ()) {
208
- if (childErrors .size () > 1 ) {
209
- Set <ValidationMessage > notAdditionalPropertiesOnly = new LinkedHashSet <>(childErrors .stream ()
210
- .filter ((ValidationMessage validationMessage ) -> !ValidatorTypeCode .ADDITIONAL_PROPERTIES .getValue ().equals (validationMessage .getType ()))
211
- .sorted ((vm1 , vm2 ) -> compareValidationMessages (vm1 , vm2 ))
212
- .collect (Collectors .toList ()));
213
- if (notAdditionalPropertiesOnly .size () > 0 ) {
214
- childErrors = notAdditionalPropertiesOnly ;
215
- }
216
- }
217
- errors .addAll (childErrors );
218
- }
219
- if (failFast ) {
220
- throw new JsonSchemaException (errors .toString ());
221
- }
222
- }
223
-
224
202
// Make sure to signal parent handlers we matched
225
203
if (errors .isEmpty ())
226
204
state .setMatchedNode (true );
@@ -240,84 +218,34 @@ else if (numberOfValidSchema < 1) {
240
218
}
241
219
}
242
220
243
- /**
244
- * Sort <code>ValidationMessage</code> by its type
245
- * @return
246
- */
247
- private static int compareValidationMessages (ValidationMessage vm1 , ValidationMessage vm2 ) {
248
- // ValidationMessage's type has smaller index in the list below has high priority
249
- final List <String > typeCodes = Arrays .asList (
250
- ValidatorTypeCode .TYPE .getValue (),
251
- ValidatorTypeCode .DATETIME .getValue (),
252
- ValidatorTypeCode .UUID .getValue (),
253
- ValidatorTypeCode .ID .getValue (),
254
- ValidatorTypeCode .EXCLUSIVE_MAXIMUM .getValue (),
255
- ValidatorTypeCode .EXCLUSIVE_MINIMUM .getValue (),
256
- ValidatorTypeCode .TRUE .getValue (),
257
- ValidatorTypeCode .FALSE .getValue (),
258
- ValidatorTypeCode .CONST .getValue (),
259
- ValidatorTypeCode .CONTAINS .getValue (),
260
- ValidatorTypeCode .PROPERTYNAMES .getValue ()
261
- );
262
-
263
- final int index1 = typeCodes .indexOf (vm1 .getType ());
264
- final int index2 = typeCodes .indexOf (vm2 .getType ());
265
-
266
- if (index1 >= 0 ) {
267
- if (index2 >= 0 ) {
268
- return Integer .compare (index1 , index2 );
269
- } else {
270
- return -1 ;
271
- }
272
- } else {
273
- if (index2 >= 0 ) {
274
- return 1 ;
275
- } else {
276
- return vm1 .getCode ().compareTo (vm2 .getCode ());
277
- }
278
- }
279
- }
280
-
281
221
private void resetValidatorState () {
282
222
ValidatorState state = (ValidatorState ) CollectorContext .getInstance ().get (ValidatorState .VALIDATOR_STATE_KEY );
283
223
state .setComplexValidator (false );
284
224
state .setMatchedNode (true );
285
225
}
286
226
287
- public List <JsonSchema > getChildSchemas () {
288
- List <JsonSchema > childJsonSchemas = new ArrayList <JsonSchema >();
289
- for (ShortcutValidator shortcutValidator : schemas ) {
290
- childJsonSchemas .add (shortcutValidator .getSchema ());
291
- }
292
- return childJsonSchemas ;
293
- }
294
-
295
227
@ Override
296
228
public Set <ValidationMessage > walk (JsonNode node , JsonNode rootNode , String at , boolean shouldValidateSchema ) {
297
229
HashSet <ValidationMessage > validationMessages = new LinkedHashSet <ValidationMessage >();
298
230
if (shouldValidateSchema ) {
299
231
validationMessages .addAll (validate (node , rootNode , at ));
300
232
} else {
301
233
for (ShortcutValidator validator : schemas ) {
302
- validator .schema .walk (node , rootNode , at , shouldValidateSchema );
234
+ validator .schema .walk (node , rootNode , at , shouldValidateSchema );
303
235
}
304
236
}
305
237
return validationMessages ;
306
238
}
307
239
308
- private ValidationMessage getMultiSchemasValidErrorMsg (String at ){
309
- String msg ="" ;
310
- for (ShortcutValidator schema : schemas ){
311
- String schemaValue = schema .getSchema ().getSchemaNode ().toString ();
312
- msg = msg .concat (schemaValue );
313
- }
314
-
240
+ private ValidationMessage getMultiSchemasValidErrorMsg (String at ) {
241
+ List <String > msgStrList = schemas .stream ().map (shortcutValidator -> shortcutValidator .getSchema ().getSchemaNode ().toString ()).collect (Collectors .toList ());
242
+ String msg = String .join (", " , msgStrList );
315
243
return ValidationMessage .of (getValidatorType ().getValue (), ValidatorTypeCode .ONE_OF , at , msg );
316
244
}
317
245
318
246
@ Override
319
247
public void preloadJsonSchema () {
320
- for (final ShortcutValidator scValidator : schemas ) {
248
+ for (final ShortcutValidator scValidator : schemas ) {
321
249
scValidator .getSchema ().initializeValidators ();
322
250
}
323
251
}
0 commit comments