Skip to content

Commit 47a79d6

Browse files
authored
Merge pull request #383 from utopia-php/int_out_of_range
Integer validator
2 parents 34e65cc + cb2b32f commit 47a79d6

File tree

5 files changed

+296
-104
lines changed

5 files changed

+296
-104
lines changed

composer.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Database/Database.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class Database
3232
public const VAR_BOOLEAN = 'boolean';
3333
public const VAR_DATETIME = 'datetime';
3434

35+
public const INT_MAX = 2147483647;
36+
public const BIG_INT_MAX = PHP_INT_MAX;
37+
public const DOUBLE_MAX = PHP_FLOAT_MAX;
38+
3539
// Relationship Types
3640
public const VAR_RELATIONSHIP = 'relationship';
3741

src/Database/Validator/Structure.php

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Utopia\Validator\Boolean;
1313
use Utopia\Validator\FloatValidator;
1414
use Utopia\Validator\Integer;
15+
use Utopia\Validator\Range;
1516
use Utopia\Validator\Text;
1617

1718
class 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

Comments
 (0)