Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 58
Integer validator#383
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
Uh oh!
There was an error while loading. Please reload this page.
Integer validator #383
Changes from all commits
fac84a3604afb02d75c91464faf3c8abc879bd255587f72adcb2b32fFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| @@ -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; | ||
| @@ -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: | ||
abnegate marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| $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: | ||
| @@ -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 | ||
| @@ -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; | ||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.