1717package com .networknt .schema ;
1818
1919import com .fasterxml .jackson .databind .JsonNode ;
20- import com .networknt .schema .utils .SetView ;
2120
2221import org .slf4j .Logger ;
2322import org .slf4j .LoggerFactory ;
@@ -54,45 +53,48 @@ public AnyOfValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath
5453 }
5554
5655 @ Override
57- public Set < ValidationMessage > validate (ExecutionContext executionContext , JsonNode node , JsonNode rootNode ,
56+ public void validate (ExecutionContext executionContext , JsonNode node , JsonNode rootNode ,
5857 JsonNodePath instanceLocation ) {
59- return validate (executionContext , node , rootNode , instanceLocation , false );
58+ validate (executionContext , node , rootNode , instanceLocation , false );
6059 }
6160
62- protected Set < ValidationMessage > validate (ExecutionContext executionContext , JsonNode node , JsonNode rootNode ,
61+ protected void validate (ExecutionContext executionContext , JsonNode node , JsonNode rootNode ,
6362 JsonNodePath instanceLocation , boolean walk ) {
6463 debug (logger , executionContext , node , rootNode , instanceLocation );
6564
6665 if (this .validationContext .getConfig ().isDiscriminatorKeywordEnabled ()) {
6766 executionContext .enterDiscriminatorContext (new DiscriminatorContext (), instanceLocation );
6867 }
69- SetView <ValidationMessage > allErrors = null ;
70-
7168 int numberOfValidSubSchemas = 0 ;
69+ List <ValidationMessage > existingErrors = executionContext .getErrors ();
70+ List <ValidationMessage > allErrors = null ;
71+ List <ValidationMessage > errors = new ArrayList <>();
72+ executionContext .setErrors (errors );
7273 try {
7374 // Save flag as nested schema evaluation shouldn't trigger fail fast
7475 boolean failFast = executionContext .isFailFast ();
7576 try {
7677 executionContext .setFailFast (false );
7778 for (JsonSchema schema : this .schemas ) {
78- Set < ValidationMessage > errors = Collections . emptySet ();
79+ errors . clear ();
7980 TypeValidator typeValidator = schema .getTypeValidator ();
8081 if (typeValidator != null ) {
8182 // If schema has type validator and node type doesn't match with schemaType then
8283 // ignore it
8384 // For union type, it is a must to call TypeValidator
8485 if (typeValidator .getSchemaType () != JsonType .UNION && !typeValidator .equalsToSchemaType (node )) {
86+ typeValidator .validate (executionContext , node , rootNode , instanceLocation );
8587 if (allErrors == null ) {
86- allErrors = new SetView <>();
88+ allErrors = new ArrayList <>();
8789 }
88- allErrors .union ( typeValidator . validate ( executionContext , node , rootNode , instanceLocation ) );
90+ allErrors .addAll ( errors );
8991 continue ;
9092 }
9193 }
9294 if (!walk ) {
93- errors = schema .validate (executionContext , node , rootNode , instanceLocation );
95+ schema .validate (executionContext , node , rootNode , instanceLocation );
9496 } else {
95- errors = schema .walk (executionContext , node , rootNode , instanceLocation , true );
97+ schema .walk (executionContext , node , rootNode , instanceLocation , true );
9698 }
9799
98100 // check if any validation errors have occurred
@@ -105,8 +107,8 @@ protected Set<ValidationMessage> validate(ExecutionContext executionContext, Jso
105107 && canShortCircuit () && canShortCircuit (executionContext )) {
106108 // Clear all errors. Note that this is checked in finally.
107109 allErrors = null ;
108- // return empty errors.
109- return errors ;
110+ executionContext . setErrors ( existingErrors );
111+ return ;
110112 } else if (this .validationContext .getConfig ().isDiscriminatorKeywordEnabled ()) {
111113 DiscriminatorContext currentDiscriminatorContext = executionContext .getCurrentDiscriminatorContext ();
112114 if (currentDiscriminatorContext .isDiscriminatorMatchFound ()
@@ -116,24 +118,25 @@ && canShortCircuit() && canShortCircuit(executionContext)) {
116118 // which is generally discarded as it returns errors but the allErrors
117119 // is getting processed in finally
118120 if (allErrors == null ) {
119- allErrors = new SetView <>();
121+ allErrors = new ArrayList <>();
120122 }
121- allErrors .union (Collections
122- .singleton (message ().instanceNode (node ).instanceLocation (instanceLocation )
123- .locale (executionContext .getExecutionConfig ().getLocale ())
124- .failFast (executionContext .isFailFast ()).arguments (DISCRIMINATOR_REMARK )
125- .build ()));
123+ allErrors .add (message ().instanceNode (node ).instanceLocation (instanceLocation )
124+ .locale (executionContext .getExecutionConfig ().getLocale ())
125+ .failFast (executionContext .isFailFast ()).arguments (DISCRIMINATOR_REMARK )
126+ .build ());
126127 } else {
127128 // Clear all errors. Note that this is checked in finally.
128129 allErrors = null ;
129130 }
130- return errors ;
131+ existingErrors .addAll (errors );
132+ executionContext .setErrors (existingErrors );
133+ return ;
131134 }
132135 }
133136 if (allErrors == null ) {
134- allErrors = new SetView <>();
137+ allErrors = new ArrayList <>();
135138 }
136- allErrors .union (errors );
139+ allErrors .addAll (errors );
137140 }
138141 } finally {
139142 // Restore flag
@@ -143,32 +146,38 @@ && canShortCircuit() && canShortCircuit(executionContext)) {
143146 if (this .validationContext .getConfig ().isDiscriminatorKeywordEnabled ()
144147 && executionContext .getCurrentDiscriminatorContext ().isActive ()
145148 && !executionContext .getCurrentDiscriminatorContext ().isDiscriminatorIgnore ()) {
146- return Collections . singleton (message ().instanceNode (node ).instanceLocation (instanceLocation )
149+ existingErrors . add (message ().instanceNode (node ).instanceLocation (instanceLocation )
147150 .locale (executionContext .getExecutionConfig ().getLocale ())
148151 .arguments (
149152 "based on the provided discriminator. No alternative could be chosen based on the discriminator property" )
150153 .build ());
154+ executionContext .setErrors (existingErrors );
155+ return ;
151156 }
152157 } finally {
153158 if (this .validationContext .getConfig ().isDiscriminatorKeywordEnabled ()) {
154159 executionContext .leaveDiscriminatorContextImmediately (instanceLocation );
155160 }
156161 }
157162 if (numberOfValidSubSchemas >= 1 ) {
158- return Collections .emptySet ();
163+ executionContext .setErrors (existingErrors );
164+ } else {
165+ if (allErrors != null ) {
166+ existingErrors .addAll (allErrors );
167+ }
168+ executionContext .setErrors (existingErrors );
159169 }
160- return allErrors != null ? allErrors : Collections .emptySet ();
161170 }
162171
163172 @ Override
164- public Set < ValidationMessage > walk (ExecutionContext executionContext , JsonNode node , JsonNode rootNode , JsonNodePath instanceLocation , boolean shouldValidateSchema ) {
173+ public void walk (ExecutionContext executionContext , JsonNode node , JsonNode rootNode , JsonNodePath instanceLocation , boolean shouldValidateSchema ) {
165174 if (shouldValidateSchema && node != null ) {
166- return validate (executionContext , node , rootNode , instanceLocation , true );
175+ validate (executionContext , node , rootNode , instanceLocation , true );
176+ return ;
167177 }
168178 for (JsonSchema schema : this .schemas ) {
169179 schema .walk (executionContext , node , rootNode , instanceLocation , false );
170180 }
171- return Collections .emptySet ();
172181 }
173182
174183 /**
0 commit comments