fix: do the validation via JsonMapper instead

This commit is contained in:
b1ek 2024-08-30 17:32:26 +10:00
parent b363bf9fc2
commit 0bfdfc9b4e
Signed by: blek
GPG Key ID: 14546221E3595D0C
2 changed files with 9 additions and 29 deletions

View File

@ -1,29 +1,17 @@
<?php <?php
namespace App\Facade\Filters; namespace App\Facade\Filters;
use App\Facade\Filters\Pagination; use App\Facade\Filters\Pagination;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Validation\Rule; use Illuminate\Http\Exceptions\HttpResponseException;
use Error;
use JsonMapper; use JsonMapper;
class Filters class Filters
{ {
public static function rules()
{
return [
'filters.*.column' => [ 'required', 'string' ],
'filters.*.type' => [ 'required', Rule::enum(FilterTypeEnum::class) ],
'filters.*.filter' => [ 'required', 'nullable', 'string' ],
'orders.*.by' => [ 'required', 'string' ],
'orders.*.sort' => [ 'required', Rule::enum(OrderTypeEnum::class) ],
'pagination.size' => 'number',
'pagination.page' => 'number',
];
}
/** /**
* @var Filter[] * @var Filter[]
*/ */
@ -47,7 +35,11 @@ class Filters
$data->orders ??= []; $data->orders ??= [];
$data->pagination ??= null; $data->pagination ??= null;
return $mapper->map($data, \App\Facade\Filters\Filters::class); try {
return $mapper->map($data, \App\Facade\Filters\Filters::class);
} catch (Error $err) {
throw new HttpResponseException(response()->json([ 'unknown' => $err->getMessage() ], 422));
}
} }
public function apply(Builder $builder) public function apply(Builder $builder)

View File

@ -1,12 +0,0 @@
<?php
namespace App\Http\Requests;
use App\Facade\Filters\Filters;
class AuthorizedFilterListRequest extends AuthorizedRequest
{
public function rules()
{
return Filters::rules();
}
}