1212use Utopia \Validator \Boolean ;
1313use Utopia \Validator \FloatValidator ;
1414use Utopia \Validator \Integer ;
15+ use Utopia \Validator \Range ;
1516use Utopia \Validator \Text ;
1617
1718class Structure extends Validator
@@ -249,6 +250,8 @@ public function isValid($document): bool
249250 $ array = $ attribute ['array ' ] ?? false ;
250251 $ format = $ attribute ['format ' ] ?? '' ;
251252 $ required = $ attribute ['required ' ] ?? false ;
253+ $ size = $ attribute ['size ' ] ?? 0 ;
254+ $ signed = $ attribute ['signed ' ] ?? true ;
252255
253256 if ($ required === false && is_null ($ value )) { // Allow null value to optional params
254257 continue ;
@@ -258,26 +261,34 @@ public function isValid($document): bool
258261 continue ;
259262 }
260263
264+ $ validators = [];
265+
261266 switch ($ type ) {
262267 case Database::VAR_STRING :
263- $ size = $ attribute ['size ' ] ?? 0 ;
264- $ validator = new Text ($ size , min: 0 );
268+ $ validators [] = new Text ($ size , min: 0 );
265269 break ;
266270
267271 case Database::VAR_INTEGER :
268- $ validator = new Integer ();
272+ // We need both Integer and Range because Range implicitly casts non-numeric values
273+ $ validators [] = new Integer ();
274+ $ max = $ size >= 8 ? Database::BIG_INT_MAX : Database::INT_MAX ;
275+ $ min = $ signed ? -$ max : 0 ;
276+ $ validators [] = new Range ($ min , $ max , Database::VAR_INTEGER );
269277 break ;
270278
271279 case Database::VAR_FLOAT :
272- $ validator = new FloatValidator ();
280+ // We need both Float and Range because Range implicitly casts non-numeric values
281+ $ validators [] = new FloatValidator ();
282+ $ min = $ signed ? -Database::DOUBLE_MAX : 0 ;
283+ $ validators [] = new Range ($ min , Database::DOUBLE_MAX , Database::VAR_FLOAT );
273284 break ;
274285
275286 case Database::VAR_BOOLEAN :
276- $ validator = new Boolean ();
287+ $ validators [] = new Boolean ();
277288 break ;
278289
279290 case Database::VAR_DATETIME :
280- $ validator = new DatetimeValidator ();
291+ $ validators [] = new DatetimeValidator ();
281292 break ;
282293
283294 default :
@@ -291,7 +302,7 @@ public function isValid($document): bool
291302 if ($ format ) {
292303 // Format encoded as json string containing format name and relevant format options
293304 $ format = self ::getFormat ($ format , $ type );
294- $ validator = $ format ['callback ' ]($ attribute );
305+ $ validators [] = $ format ['callback ' ]($ attribute );
295306 }
296307
297308 if ($ array ) { // Validate attribute type for arrays - format for arrays handled separately
@@ -308,15 +319,19 @@ public function isValid($document): bool
308319 continue ;
309320 }
310321
311- if (!$ validator ->isValid ($ child )) {
312- $ this ->message = 'Attribute " ' .$ key .'[ \'' .$ x .'\']" has invalid ' .$ label .'. ' .$ validator ->getDescription ();
313- return false ;
322+ foreach ($ validators as $ validator ) {
323+ if (!$ validator ->isValid ($ child )) {
324+ $ this ->message = 'Attribute " ' .$ key .'[ \'' .$ x .'\']" has invalid ' .$ label .'. ' .$ validator ->getDescription ();
325+ return false ;
326+ }
314327 }
315328 }
316329 } else {
317- if (!$ validator ->isValid ($ value )) {
318- $ this ->message = 'Attribute " ' .$ key .'" has invalid ' .$ label .'. ' .$ validator ->getDescription ();
319- return false ;
330+ foreach ($ validators as $ validator ) {
331+ if (!$ validator ->isValid ($ value )) {
332+ $ this ->message = 'Attribute " ' .$ key .'" has invalid ' .$ label .'. ' .$ validator ->getDescription ();
333+ return false ;
334+ }
320335 }
321336 }
322337 }
0 commit comments