Skip to content

Commit

Permalink
fix validation exception
Browse files Browse the repository at this point in the history
  • Loading branch information
daycry committed Apr 28, 2023
1 parent f94fb6b commit 7891ae2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
7 changes: 5 additions & 2 deletions src/Exceptions/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

namespace Daycry\RestFul\Exceptions;

use RuntimeException;

class ValidationException extends RuntimeException
{
public static function validationtMethodParamsError($param)
{
$parser = \Config\Services::parser();
return new self($parser->setData(array( 'param' => $param ))->renderString(lang('RestFul.invalidParamsForMethod')));
}

public static function validationData()
{
return new self('');
}
}
39 changes: 19 additions & 20 deletions src/RestFul.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use CodeIgniter\Validation\ValidationInterface;
use Config\Services;
use Config\Validation;
use CodeIgniter\Router\Router;
Expand Down Expand Up @@ -150,25 +149,25 @@ public function __destruct()

protected function validation(string $rules, $data = null, ?Validation $config = null, bool $getShared = true, bool $filter = false)
{
$config ??= config('Validation');
$data ??= $this->content;

$this->validator = Services::validation($config, $getShared);

$content = json_decode(json_encode($data), true);
if (!$this->validator->run($content, $rules)) {
throw new ValidationException();
}

if ($filter) {
if ($data) {
foreach ($data as $key => $value) {
if (!array_key_exists($key, $config->{$rules})) {
throw ValidationException::validationtMethodParamsError($key);
}
}
}
}
$config ??= config('Validation');
$data ??= $this->content;

$this->validator = Services::validation($config, $getShared);

$content = json_decode(json_encode($data), true);
if (!$this->validator->run($content, $rules)) {
throw ValidationException::validationData();
}

if ($filter) {
if ($data) {
foreach ($data as $key => $value) {
if (!array_key_exists($key, $config->{$rules})) {
throw ValidationException::validationtMethodParamsError($key);
}
}
}
}
}

/**
Expand Down

0 comments on commit 7891ae2

Please sign in to comment.