Skip to content

Commit

Permalink
Merge pull request #383 from utopia-php/int_out_of_range
Browse files Browse the repository at this point in the history
Integer validator
  • Loading branch information
abnegate authored Jan 31, 2024
2 parents 34e65cc + cb2b32f commit 47a79d6
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 104 deletions.
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class Database
public const VAR_BOOLEAN = 'boolean';
public const VAR_DATETIME = 'datetime';

public const INT_MAX = 2147483647;
public const BIG_INT_MAX = PHP_INT_MAX;
public const DOUBLE_MAX = PHP_FLOAT_MAX;

// Relationship Types
public const VAR_RELATIONSHIP = 'relationship';

Expand Down
41 changes: 28 additions & 13 deletions src/Database/Validator/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Utopia\Validator\Boolean;
use Utopia\Validator\FloatValidator;
use Utopia\Validator\Integer;
use Utopia\Validator\Range;
use Utopia\Validator\Text;

class Structure extends Validator
Expand Down Expand Up @@ -249,6 +250,8 @@ public function isValid($document): bool
$array = $attribute['array'] ?? false;
$format = $attribute['format'] ?? '';
$required = $attribute['required'] ?? false;
$size = $attribute['size'] ?? 0;
$signed = $attribute['signed'] ?? true;

if ($required === false && is_null($value)) { // Allow null value to optional params
continue;
Expand All @@ -258,26 +261,34 @@ public function isValid($document): bool
continue;
}

$validators = [];

switch ($type) {
case Database::VAR_STRING:
$size = $attribute['size'] ?? 0;
$validator = new Text($size, min: 0);
$validators[] = new Text($size, min: 0);
break;

case Database::VAR_INTEGER:
$validator = new Integer();
// We need both Integer and Range because Range implicitly casts non-numeric values
$validators[] = new Integer();
$max = $size >= 8 ? Database::BIG_INT_MAX : Database::INT_MAX;
$min = $signed ? -$max : 0;
$validators[] = new Range($min, $max, Database::VAR_INTEGER);
break;

case Database::VAR_FLOAT:
$validator = new FloatValidator();
// We need both Float and Range because Range implicitly casts non-numeric values
$validators[] = new FloatValidator();
$min = $signed ? -Database::DOUBLE_MAX : 0;
$validators[] = new Range($min, Database::DOUBLE_MAX, Database::VAR_FLOAT);
break;

case Database::VAR_BOOLEAN:
$validator = new Boolean();
$validators[] = new Boolean();
break;

case Database::VAR_DATETIME:
$validator = new DatetimeValidator();
$validators[] = new DatetimeValidator();
break;

default:
Expand All @@ -291,7 +302,7 @@ public function isValid($document): bool
if ($format) {
// Format encoded as json string containing format name and relevant format options
$format = self::getFormat($format, $type);
$validator = $format['callback']($attribute);
$validators[] = $format['callback']($attribute);
}

if ($array) { // Validate attribute type for arrays - format for arrays handled separately
Expand All @@ -308,15 +319,19 @@ public function isValid($document): bool
continue;
}

if (!$validator->isValid($child)) {
$this->message = 'Attribute "'.$key.'[\''.$x.'\']" has invalid '.$label.'. '.$validator->getDescription();
return false;
foreach ($validators as $validator) {
if (!$validator->isValid($child)) {
$this->message = 'Attribute "'.$key.'[\''.$x.'\']" has invalid '.$label.'. '.$validator->getDescription();
return false;
}
}
}
} else {
if (!$validator->isValid($value)) {
$this->message = 'Attribute "'.$key.'" has invalid '.$label.'. '.$validator->getDescription();
return false;
foreach ($validators as $validator) {
if (!$validator->isValid($value)) {
$this->message = 'Attribute "'.$key.'" has invalid '.$label.'. '.$validator->getDescription();
return false;
}
}
}
}
Expand Down
Loading

0 comments on commit 47a79d6

Please sign in to comment.