Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integer validator #383

Merged
merged 8 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 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
39 changes: 26 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,32 @@ 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();
$validators[] = new Integer();
abnegate marked this conversation as resolved.
Show resolved Hide resolved
$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:
abnegate marked this conversation as resolved.
Show resolved Hide resolved
$validator = new FloatValidator();
$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 +300,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)];
abnegate marked this conversation as resolved.
Show resolved Hide resolved
}

if ($array) { // Validate attribute type for arrays - format for arrays handled separately
Expand All @@ -308,15 +317,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
Loading