Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 30 additions & 4 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
use Utopia\Telemetry\Adapter\None as NoTelemetry;
use Utopia\Telemetry\Histogram;
use Utopia\Telemetry\UpDownCounter;
use Utopia\Validator\ArrayList;
use Utopia\Validator\Boolean;
use Utopia\Validator\FloatValidator;
use Utopia\Validator\Integer;
use Utopia\Validator\Text;

class App
{
Expand Down Expand Up @@ -718,7 +723,7 @@ protected function getArguments(Hook $hook, array $values, array $requestParams)
}
if (\is_array($value)) {
$validator = $param['validator'];
$isArrayList = $validator instanceof \Utopia\Validator\ArrayList;
$isArrayList = $validator instanceof ArrayList;

if ($isArrayList) {
try {
Expand Down Expand Up @@ -747,7 +752,26 @@ protected function getArguments(Hook $hook, array $values, array $requestParams)
!($param['optional'] && $value === null) &&
$paramExists
) {
$this->validate($key, $param, $value);
$validator = $this->validate($key, $param, $value);

if ($existsInRequest && $value !== null) {
if ($validator instanceof Boolean) {
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
if ($value === null) {
throw new Exception('Invalid boolean value for param "' . $key . '"', 400);
}
} elseif ($validator instanceof Integer && \is_string($value)) {
if (\is_numeric($value)) {
$value = (int)$value;
}
} elseif ($validator instanceof FloatValidator && \is_string($value)) {
if (\is_numeric($value)) {
$value = (float)$value;
}
} elseif ($validator instanceof Text && !\is_string($value)) {
$value = (string)$value;
}
}
}

$hook->setParamValue($key, $value);
Expand Down Expand Up @@ -933,11 +957,11 @@ private function runInternal(Request $request, Response $response): static
* @param string $key
* @param array $param
* @param mixed $value
* @return void
* @return Validator
*
* @throws Exception
*/
protected function validate(string $key, array $param, mixed $value): void
protected function validate(string $key, array $param, mixed $value): Validator
{
$validator = $param['validator']; // checking whether the class exists

Expand All @@ -952,6 +976,8 @@ protected function validate(string $key, array $param, mixed $value): void
if (!$validator->isValid($value)) {
throw new Exception('Invalid `' . $key . '` param: ' . $validator->getDescription(), 400);
}

return $validator;
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public function testCanGetDefaultValueWithFunction(): void
echo $x;
});

\ob_start();
$request = new UtopiaRequestTest();
$request::_setParams(['x' => 'count']);
$this->app->execute($route, $request, new Response());
Expand Down