@@ -527,16 +527,20 @@ private Optional<ValidationError> checkAllOf(Object value, List<Schema> parts, S
527527 }
528528
529529 private Optional <ValidationError > checkAnyOf (Object value , List <Schema > options , String pointer ) {
530- List <ValidationError > failures = new ArrayList <>() ;
530+ List <ValidationError > failures = null ;
531531 for (Schema o : options ) {
532532 Optional <ValidationError > result = check (value , o , pointer );
533533 if (result .isEmpty ()) {
534534 return OK ;
535535 }
536+ if (failures == null ) {
537+ failures = new ArrayList <>(options .size () - 1 );
538+ }
536539 failures .add (result .get ());
537540 }
541+ List <ValidationError > branches = failures != null ? failures : List .of ();
538542 return Optional .of (
539- new ValidationError (pointer , "anyOf" , "did not match any anyOf branch" , value , failures ));
543+ new ValidationError (pointer , "anyOf" , "did not match any anyOf branch" , value , branches ));
540544 }
541545
542546 private Optional <ValidationError > checkOneOf (Object value , List <Schema > options , String pointer ) {
@@ -559,6 +563,8 @@ private Optional<ValidationError> checkOneOf(Object value, List<Schema> options,
559563 "oneOf" ,
560564 "matched " + matched + " of " + options .size () + " oneOf branches" ,
561565 value ,
566+ // Ambiguous match (matched > 1): the non-matching branches' errors are noise — omit
567+ // them.
562568 matched == 0 ? failures : List .of ()));
563569 }
564570
0 commit comments