17
17
package com .networknt .schema ;
18
18
19
19
import com .fasterxml .jackson .databind .JsonNode ;
20
- import com .networknt .schema .utils .SetView ;
21
20
22
21
import org .slf4j .Logger ;
23
22
import org .slf4j .LoggerFactory ;
@@ -54,45 +53,48 @@ public AnyOfValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath
54
53
}
55
54
56
55
@ Override
57
- public Set < ValidationMessage > validate (ExecutionContext executionContext , JsonNode node , JsonNode rootNode ,
56
+ public void validate (ExecutionContext executionContext , JsonNode node , JsonNode rootNode ,
58
57
JsonNodePath instanceLocation ) {
59
- return validate (executionContext , node , rootNode , instanceLocation , false );
58
+ validate (executionContext , node , rootNode , instanceLocation , false );
60
59
}
61
60
62
- protected Set < ValidationMessage > validate (ExecutionContext executionContext , JsonNode node , JsonNode rootNode ,
61
+ protected void validate (ExecutionContext executionContext , JsonNode node , JsonNode rootNode ,
63
62
JsonNodePath instanceLocation , boolean walk ) {
64
63
debug (logger , executionContext , node , rootNode , instanceLocation );
65
64
66
65
if (this .validationContext .getConfig ().isDiscriminatorKeywordEnabled ()) {
67
66
executionContext .enterDiscriminatorContext (new DiscriminatorContext (), instanceLocation );
68
67
}
69
- SetView <ValidationMessage > allErrors = null ;
70
-
71
68
int numberOfValidSubSchemas = 0 ;
69
+ List <ValidationMessage > existingErrors = executionContext .getErrors ();
70
+ List <ValidationMessage > allErrors = null ;
71
+ List <ValidationMessage > errors = new ArrayList <>();
72
+ executionContext .setErrors (errors );
72
73
try {
73
74
// Save flag as nested schema evaluation shouldn't trigger fail fast
74
75
boolean failFast = executionContext .isFailFast ();
75
76
try {
76
77
executionContext .setFailFast (false );
77
78
for (JsonSchema schema : this .schemas ) {
78
- Set < ValidationMessage > errors = Collections . emptySet ();
79
+ errors . clear ();
79
80
TypeValidator typeValidator = schema .getTypeValidator ();
80
81
if (typeValidator != null ) {
81
82
// If schema has type validator and node type doesn't match with schemaType then
82
83
// ignore it
83
84
// For union type, it is a must to call TypeValidator
84
85
if (typeValidator .getSchemaType () != JsonType .UNION && !typeValidator .equalsToSchemaType (node )) {
86
+ typeValidator .validate (executionContext , node , rootNode , instanceLocation );
85
87
if (allErrors == null ) {
86
- allErrors = new SetView <>();
88
+ allErrors = new ArrayList <>();
87
89
}
88
- allErrors .union ( typeValidator . validate ( executionContext , node , rootNode , instanceLocation ) );
90
+ allErrors .addAll ( errors );
89
91
continue ;
90
92
}
91
93
}
92
94
if (!walk ) {
93
- errors = schema .validate (executionContext , node , rootNode , instanceLocation );
95
+ schema .validate (executionContext , node , rootNode , instanceLocation );
94
96
} else {
95
- errors = schema .walk (executionContext , node , rootNode , instanceLocation , true );
97
+ schema .walk (executionContext , node , rootNode , instanceLocation , true );
96
98
}
97
99
98
100
// check if any validation errors have occurred
@@ -105,8 +107,8 @@ protected Set<ValidationMessage> validate(ExecutionContext executionContext, Jso
105
107
&& canShortCircuit () && canShortCircuit (executionContext )) {
106
108
// Clear all errors. Note that this is checked in finally.
107
109
allErrors = null ;
108
- // return empty errors.
109
- return errors ;
110
+ executionContext . setErrors ( existingErrors );
111
+ return ;
110
112
} else if (this .validationContext .getConfig ().isDiscriminatorKeywordEnabled ()) {
111
113
DiscriminatorContext currentDiscriminatorContext = executionContext .getCurrentDiscriminatorContext ();
112
114
if (currentDiscriminatorContext .isDiscriminatorMatchFound ()
@@ -116,24 +118,25 @@ && canShortCircuit() && canShortCircuit(executionContext)) {
116
118
// which is generally discarded as it returns errors but the allErrors
117
119
// is getting processed in finally
118
120
if (allErrors == null ) {
119
- allErrors = new SetView <>();
121
+ allErrors = new ArrayList <>();
120
122
}
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 ());
126
127
} else {
127
128
// Clear all errors. Note that this is checked in finally.
128
129
allErrors = null ;
129
130
}
130
- return errors ;
131
+ existingErrors .addAll (errors );
132
+ executionContext .setErrors (existingErrors );
133
+ return ;
131
134
}
132
135
}
133
136
if (allErrors == null ) {
134
- allErrors = new SetView <>();
137
+ allErrors = new ArrayList <>();
135
138
}
136
- allErrors .union (errors );
139
+ allErrors .addAll (errors );
137
140
}
138
141
} finally {
139
142
// Restore flag
@@ -143,32 +146,38 @@ && canShortCircuit() && canShortCircuit(executionContext)) {
143
146
if (this .validationContext .getConfig ().isDiscriminatorKeywordEnabled ()
144
147
&& executionContext .getCurrentDiscriminatorContext ().isActive ()
145
148
&& !executionContext .getCurrentDiscriminatorContext ().isDiscriminatorIgnore ()) {
146
- return Collections . singleton (message ().instanceNode (node ).instanceLocation (instanceLocation )
149
+ existingErrors . add (message ().instanceNode (node ).instanceLocation (instanceLocation )
147
150
.locale (executionContext .getExecutionConfig ().getLocale ())
148
151
.arguments (
149
152
"based on the provided discriminator. No alternative could be chosen based on the discriminator property" )
150
153
.build ());
154
+ executionContext .setErrors (existingErrors );
155
+ return ;
151
156
}
152
157
} finally {
153
158
if (this .validationContext .getConfig ().isDiscriminatorKeywordEnabled ()) {
154
159
executionContext .leaveDiscriminatorContextImmediately (instanceLocation );
155
160
}
156
161
}
157
162
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 );
159
169
}
160
- return allErrors != null ? allErrors : Collections .emptySet ();
161
170
}
162
171
163
172
@ 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 ) {
165
174
if (shouldValidateSchema && node != null ) {
166
- return validate (executionContext , node , rootNode , instanceLocation , true );
175
+ validate (executionContext , node , rootNode , instanceLocation , true );
176
+ return ;
167
177
}
168
178
for (JsonSchema schema : this .schemas ) {
169
179
schema .walk (executionContext , node , rootNode , instanceLocation , false );
170
180
}
171
- return Collections .emptySet ();
172
181
}
173
182
174
183
/**
0 commit comments