@@ -63,23 +63,51 @@ protected function validateItems($value, $schema = null, JsonPointer $path = nul
6363 {
6464 if (is_object ($ schema ->items )) {
6565 // just one type definition for the whole array
66- foreach ($ value as $ k => $ v ) {
67- $ initErrors = $ this ->getErrors ();
6866
69- // First check if its defined in "items"
70- $ this ->checkUndefined ($ v , $ schema ->items , $ path , $ k );
67+ if (isset ($ schema ->items ->type )
68+ && (
69+ $ schema ->items ->type == 'string '
70+ || $ schema ->items ->type == 'number '
71+ || $ schema ->items ->type == 'integer '
72+ )
73+ && !isset ($ schema ->additionalItems )
74+ ) {
75+ // performance optimization
76+ $ type = $ schema ->items ->type ;
77+ $ validator = $ this ->factory ->createInstanceFor ($ type === 'integer ' ? 'number ' : $ type );
78+
79+ foreach ($ value as $ k => $ v ) {
80+ $ k_path = $ this ->incrementPath ($ path , $ k );
7181
72- // Recheck with "additionalItems" if the first test fails
73- if (count ($ initErrors ) < count ($ this ->getErrors ()) && (isset ($ schema ->additionalItems ) && $ schema ->additionalItems !== false )) {
74- $ secondErrors = $ this ->getErrors ();
75- $ this ->checkUndefined ($ v , $ schema ->additionalItems , $ path , $ k );
82+ if (($ type === 'string ' && !is_string ($ v ))
83+ || ($ type === 'number ' && !(is_numeric ($ v ) && !is_string ($ v )))
84+ || ($ type === 'integer ' && !is_int ($ v ))
85+ ){
86+ $ this ->addError ($ k_path , ucwords (gettype ($ v )) . " value found, but $ type is required " , 'type ' );
87+ } else {
88+ $ validator ->check ($ v , $ schema , $ k_path , $ i );
89+ }
7690 }
91+ $ this ->addErrors ($ validator ->getErrors ());
92+ } else {
93+ foreach ($ value as $ k => $ v ) {
94+ $ initErrors = $ this ->getErrors ();
7795
78- // Reset errors if needed
79- if (isset ($ secondErrors ) && count ($ secondErrors ) < count ($ this ->getErrors ())) {
80- $ this ->errors = $ secondErrors ;
81- } elseif (isset ($ secondErrors ) && count ($ secondErrors ) === count ($ this ->getErrors ())) {
82- $ this ->errors = $ initErrors ;
96+ // First check if its defined in "items"
97+ $ this ->checkUndefined ($ v , $ schema ->items , $ path , $ k );
98+
99+ // Recheck with "additionalItems" if the first test fails
100+ if (count ($ initErrors ) < count ($ this ->getErrors ()) && (isset ($ schema ->additionalItems ) && $ schema ->additionalItems !== false )) {
101+ $ secondErrors = $ this ->getErrors ();
102+ $ this ->checkUndefined ($ v , $ schema ->additionalItems , $ path , $ k );
103+ }
104+
105+ // Reset errors if needed
106+ if (isset ($ secondErrors ) && count ($ secondErrors ) < count ($ this ->getErrors ())) {
107+ $ this ->errors = $ secondErrors ;
108+ } elseif (isset ($ secondErrors ) && count ($ secondErrors ) === count ($ this ->getErrors ())) {
109+ $ this ->errors = $ initErrors ;
110+ }
83111 }
84112 }
85113 } else {
0 commit comments