1717package com .networknt .schema ;
1818
1919import com .fasterxml .jackson .databind .JsonNode ;
20+ import com .networknt .schema .walk .DefaultItemWalkListenerRunner ;
21+ import com .networknt .schema .walk .WalkListenerRunner ;
22+
2023import org .slf4j .Logger ;
2124import org .slf4j .LoggerFactory ;
2225
@@ -30,17 +33,21 @@ public class ItemsValidator extends BaseJsonValidator implements JsonValidator {
3033 private List <JsonSchema > tupleSchema ;
3134 private boolean additionalItems = true ;
3235 private JsonSchema additionalSchema ;
36+ private WalkListenerRunner arrayItemWalkListenerRunner ;
37+ private ValidationContext validationContext ;
3338
34- public ItemsValidator (String schemaPath , JsonNode schemaNode , JsonSchema parentSchema , ValidationContext validationContext ) {
39+ public ItemsValidator (String schemaPath , JsonNode schemaNode , JsonSchema parentSchema ,
40+ ValidationContext validationContext ) {
3541 super (schemaPath , schemaNode , parentSchema , ValidatorTypeCode .ITEMS , validationContext );
3642 if (schemaNode .isObject () || schemaNode .isBoolean ()) {
37- schema = new JsonSchema (validationContext , schemaPath , parentSchema .getCurrentUri (), schemaNode , parentSchema )
38- .initialize ();
43+ schema = new JsonSchema (validationContext , schemaPath , parentSchema .getCurrentUri (), schemaNode ,
44+ parentSchema ) .initialize ();
3945 } else {
4046 tupleSchema = new ArrayList <JsonSchema >();
4147 for (JsonNode s : schemaNode ) {
42- tupleSchema .add (new JsonSchema (validationContext , schemaPath , parentSchema .getCurrentUri (), s , parentSchema )
43- .initialize ());
48+ tupleSchema .add (
49+ new JsonSchema (validationContext , schemaPath , parentSchema .getCurrentUri (), s , parentSchema )
50+ .initialize ());
4451 }
4552
4653 JsonNode addItemNode = getParentSchema ().getSchemaNode ().get (PROPERTY_ADDITIONAL_ITEMS );
@@ -49,10 +56,13 @@ public ItemsValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
4956 additionalItems = addItemNode .asBoolean ();
5057 } else if (addItemNode .isObject ()) {
5158 additionalSchema = new JsonSchema (validationContext , parentSchema .getCurrentUri (), addItemNode )
52- .initialize ();
59+ .initialize ();
5360 }
5461 }
5562 }
63+ arrayItemWalkListenerRunner = new DefaultItemWalkListenerRunner (config .getArrayItemWalkListeners ());
64+
65+ this .validationContext = validationContext ;
5666
5767 parseErrorCode (getValidatorType ().getErrorCodeKey ());
5868 }
@@ -100,57 +110,64 @@ private void doValidate(Set<ValidationMessage> errors, int i, JsonNode node, Jso
100110 }
101111 }
102112 }
103-
104- @ Override
105- public Set <ValidationMessage > walk (JsonNode node , JsonNode rootNode , String at , boolean shouldValidateSchema ) {
106- HashSet <ValidationMessage > validationMessages = new LinkedHashSet <ValidationMessage >();
107- if (node != null && node .isArray ()) {
108- int i = 0 ;
109- for (JsonNode n : node ) {
110- doWalk (validationMessages , i , n , rootNode , at , shouldValidateSchema );
111- i ++;
112- }
113- } else {
114- doWalk (validationMessages , 0 , node , rootNode , at , shouldValidateSchema );
115- }
116- return validationMessages ;
117- }
118-
119- private void doWalk (HashSet <ValidationMessage > validationMessages , int i , JsonNode node , JsonNode rootNode ,
120- String at , boolean shouldValidateSchema ) {
121- if (schema != null ) {
122- if (shouldValidateSchema ) {
123- validationMessages .addAll (schema .validate (node , rootNode , at ));
124- }
125- // Walk the schema.
126- validationMessages .addAll (schema .walk (node , rootNode , at + "[" + i + "]" , shouldValidateSchema ));
127- }
128-
129- if (tupleSchema != null ) {
130- if (i < tupleSchema .size ()) {
131- if (shouldValidateSchema ) {
132- validationMessages .addAll (tupleSchema .get (i ).validate (node , rootNode , at ));
133- }
134- // walk tuple schema
135- validationMessages .addAll (tupleSchema .get (i ).walk (node , rootNode , at + "[" + i + "]" , shouldValidateSchema ));
136- } else {
137- if (additionalSchema != null ) {
138- if (shouldValidateSchema ) {
139- validationMessages .addAll (additionalSchema .validate (node , rootNode , at ));
140- }
141- // walk additional item schema
142- validationMessages .addAll (additionalSchema .walk (node , rootNode , at + "[" + i + "]" , shouldValidateSchema ));
143- }
144- }
145- }
146- }
147-
148- public List <JsonSchema > getTupleSchema () {
149- return this .tupleSchema ;
150- }
151-
152- public JsonSchema getSchema () {
153- return schema ;
154- }
155-
156- }
113+
114+ @ Override
115+ public Set <ValidationMessage > walk (JsonNode node , JsonNode rootNode , String at , boolean shouldValidateSchema ) {
116+ HashSet <ValidationMessage > validationMessages = new LinkedHashSet <ValidationMessage >();
117+ if (node != null && node .isArray ()) {
118+ int i = 0 ;
119+ for (JsonNode n : node ) {
120+ doWalk (validationMessages , i , n , rootNode , at , shouldValidateSchema );
121+ i ++;
122+ }
123+ } else {
124+ doWalk (validationMessages , 0 , node , rootNode , at , shouldValidateSchema );
125+ }
126+ return validationMessages ;
127+ }
128+
129+ private void doWalk (HashSet <ValidationMessage > validationMessages , int i , JsonNode node , JsonNode rootNode ,
130+ String at , boolean shouldValidateSchema ) {
131+ if (schema != null ) {
132+ // Walk the schema.
133+ walkSchema (schema , node , rootNode , at + "[" + i + "]" , shouldValidateSchema , validationMessages );
134+ }
135+
136+ if (tupleSchema != null ) {
137+ if (i < tupleSchema .size ()) {
138+ // walk tuple schema
139+ walkSchema (tupleSchema .get (i ), node , rootNode , at + "[" + i + "]" , shouldValidateSchema ,
140+ validationMessages );
141+ } else {
142+ if (additionalSchema != null ) {
143+ // walk additional item schema
144+ walkSchema (additionalSchema , node , rootNode , at + "[" + i + "]" , shouldValidateSchema ,
145+ validationMessages );
146+ }
147+ }
148+ }
149+ }
150+
151+ private void walkSchema (JsonSchema walkSchema , JsonNode node , JsonNode rootNode , String at ,
152+ boolean shouldValidateSchema , Set <ValidationMessage > validationMessages ) {
153+ boolean executeWalk = arrayItemWalkListenerRunner .runPreWalkListeners (ValidatorTypeCode .ITEMS .getValue (), node ,
154+ rootNode , at , walkSchema .getSchemaPath (), walkSchema .getSchemaNode (), walkSchema .getParentSchema (),
155+ validationContext .getJsonSchemaFactory ());
156+ if (executeWalk ) {
157+ validationMessages .addAll (walkSchema .walk (node , rootNode , at , shouldValidateSchema ));
158+ }
159+ arrayItemWalkListenerRunner .runPostWalkListeners (ValidatorTypeCode .ITEMS .getValue (), node , rootNode , at ,
160+ walkSchema .getSchemaPath (), walkSchema .getSchemaNode (), walkSchema .getParentSchema (),
161+ validationContext .getJsonSchemaFactory (), validationMessages );
162+
163+ }
164+
165+ public List <JsonSchema > getTupleSchema () {
166+ return this .tupleSchema ;
167+ }
168+
169+ public JsonSchema getSchema () {
170+ return schema ;
171+ }
172+
173+ }
0 commit comments