Skip to content

Commit aa0e4dc

Browse files
committed
fix: nested filters validation
1 parent 08c0505 commit aa0e4dc

File tree

2 files changed

+45
-29
lines changed

2 files changed

+45
-29
lines changed

config/orion.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
return [
44
'namespaces' => [
55
'models' => 'App\\Models\\',
6-
'controllers' => 'App\\Http\\Controllers\\'
6+
'controllers' => 'App\\Http\\Controllers\\',
77
],
88
'auth' => [
9-
'guard' => 'api'
9+
'guard' => 'api',
1010
],
1111
'specs' => [
1212
'info' => [
@@ -27,26 +27,26 @@
2727
'servers' => [
2828
['url' => env('APP_URL').'/api', 'description' => 'Default Environment'],
2929
],
30-
'tags' => []
30+
'tags' => [],
3131
],
3232
'transactions' => [
3333
'enabled' => false,
3434
],
3535
'search' => [
3636
'case_sensitive' => true, // TODO: set to "false" by default in 3.0 release
37-
/*
38-
|--------------------------------------------------------------------------
39-
| Max Nested Depth
40-
|--------------------------------------------------------------------------
41-
|
42-
| This value is the maximum depth of nested filters
43-
| you will most likely need this to be maximum at 1 but
44-
| you can increase this number if necessary. Please
45-
| be aware that the depth generate dynamic rules and can slow
46-
| your application if someone sends a request with thousands of nested
47-
| filters.
48-
|
49-
*/
50-
'max_nested_depth' => 1
37+
/*
38+
|--------------------------------------------------------------------------
39+
| Max Nested Depth
40+
|--------------------------------------------------------------------------
41+
|
42+
| This value is the maximum depth of nested filters.
43+
| You will most likely need this to be maximum at 1, but
44+
| you can increase this number, if necessary. Please
45+
| be aware that the depth generate dynamic rules and can slow
46+
| your application if someone sends a request with thousands of nested
47+
| filters.
48+
|
49+
*/
50+
'max_nested_depth' => 1,
5151
],
5252
];

src/Drivers/Standard/ParamsValidator.php

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,32 @@ public function validateScopes(Request $request): void
4747

4848
public function validateFilters(Request $request): void
4949
{
50-
$max_depth = floor($this->getArrayDepth($request->all()['filters']) / 2);
51-
$config_max_nested_depth = config('orion.search.max_nested_depth', 1);
50+
$maxDepth = floor($this->getArrayDepth($request->all()['filters']) / 2);
51+
$configMaxNestedDepth = config('orion.search.max_nested_depth', 1);
5252

53-
abort_if($max_depth > $config_max_nested_depth, 422, __('Max nested depth :depth is exceeded', ['depth' => $config_max_nested_depth]));
53+
abort_if(
54+
$maxDepth > $configMaxNestedDepth,
55+
422,
56+
__('Max nested depth :depth is exceeded', ['depth' => $configMaxNestedDepth])
57+
);
5458

5559
Validator::make(
5660
$request->all(),
5761
array_merge([
5862
'filters' => ['sometimes', 'array'],
59-
], $this->getNestedRules('filters', $max_depth))
63+
], $this->getNestedRules('filters', $maxDepth))
6064
)->validate();
6165
}
6266

63-
protected function getNestedRules($prefix, $max_depth, $rules = [], $current_depth = 1) {
67+
/**
68+
* @param string $prefix
69+
* @param int $maxDepth
70+
* @param array $rules
71+
* @param int $currentDepth
72+
* @return array
73+
*/
74+
protected function getNestedRules(string $prefix, int $maxDepth, array $rules = [], int $currentDepth = 1): array
75+
{
6476
$rules = array_merge($rules, [
6577
$prefix.'.*.type' => ['sometimes', 'in:and,or'],
6678
$prefix.'.*.field' => [
@@ -73,28 +85,32 @@ protected function getNestedRules($prefix, $max_depth, $rules = [], $current_dep
7385
'in:<,<=,>,>=,=,!=,like,not like,ilike,not ilike,in,not in,all in,any in',
7486
],
7587
$prefix.'.*.value' => ['nullable'],
76-
$prefix.'.*.nested' => ['sometimes', 'array', "prohibits:{$prefix}.*.operator,{$prefix}.*.value,{$prefix}.*.field"],
88+
$prefix.'.*.nested' => ['sometimes', 'array',],
7789
]);
7890

79-
if ($max_depth >= $current_depth) {
80-
$rules = array_merge($rules, $this->getNestedRules("{$prefix}.*.nested", $max_depth, $rules, ++$current_depth));
91+
if ($maxDepth >= $currentDepth) {
92+
$rules = array_merge(
93+
$rules,
94+
$this->getNestedRules("{$prefix}.*.nested", $maxDepth, $rules, ++$currentDepth)
95+
);
8196
}
8297

8398
return $rules;
8499
}
85100

86-
protected function getArrayDepth($array) {
87-
$max_depth = 0;
101+
protected function getArrayDepth($array): int
102+
{
103+
$maxDepth = 0;
88104

89105
foreach ($array as $value) {
90106
if (is_array($value)) {
91107
$depth = $this->getArrayDepth($value) + 1;
92108

93-
$max_depth = max($depth, $max_depth);
109+
$maxDepth = max($depth, $maxDepth);
94110
}
95111
}
96112

97-
return $max_depth;
113+
return $maxDepth;
98114
}
99115

100116
public function validateSort(Request $request): void

0 commit comments

Comments
 (0)