Skip to content

Commit 360c8a0

Browse files
committed
feat: add relations validation in get and search requests
refs: #206
1 parent 16d5d20 commit 360c8a0

File tree

7 files changed

+72
-7
lines changed

7 files changed

+72
-7
lines changed

src/Generators/RequestsGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ protected function createRequest($method, $needToValidate = true, $parameters =
7676
'namespace' => $this->getNamespace('requests'),
7777
'servicesNamespace' => $this->getNamespace('services'),
7878
'entityNamespace' => $this->getModelClass($this->model),
79+
'needToValidateWith' => !is_null(Arr::first($parameters, fn ($parameter) => $parameter['name'] === 'with.*')),
7980
]);
8081

8182
$this->saveClass('requests', "{$method}{$modelName}Request",
@@ -195,7 +196,7 @@ protected function getRules($name, $type, $required, $nullable, $present): array
195196
$rules[] = 'present';
196197
}
197198

198-
if ($name === 'order_by') {
199+
if (in_array($name, ['order_by', 'with.*'])) {
199200
$rules[] = 'in:';
200201
}
201202

stubs/request.blade.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ class {{ $method }}{{ $entity }}Request extends Request
1616
public function rules(): array
1717
{
1818
@if(!empty($parameters))
19+
@if($needToValidateWith)
20+
$availableRelations = implode(',', $this->getAvailableRelations());
21+
22+
@endif
1923
return [
2024
@foreach($parameters as $parameter)
21-
'{{ $parameter['name'] }}' => '{!! implode('|', $parameter['rules']) !!}'@if ($parameter['name'] === 'order_by') . $this->getOrderableFields({{ Str::singular($entity) }}::class)@endif,
25+
'{{ $parameter['name'] }}' => '{!! implode('|', $parameter['rules']) !!}'@if ($parameter['name'] === 'order_by') . $this->getOrderableFields({{ Str::singular($entity) }}::class)@elseif($parameter['name'] === 'with.*'){{ ' . $availableRelations' }}@endif,
2226
@endforeach
2327
];
2428
@else
@@ -41,4 +45,14 @@ public function validateResolved(): void
4145
}
4246
}
4347
@endif
48+
@if($needToValidateWith)
49+
50+
//TODO: don't forget to review relations list
51+
protected function getAvailableRelations(): array
52+
{
53+
return [
54+
'role',
55+
];
56+
}
57+
@endif
4458
}

tests/fixtures/CommandTest/get_request.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ class GetPostRequest extends Request
1010
{
1111
public function rules(): array
1212
{
13+
$availableRelations = implode(',', $this->getAvailableRelations());
14+
1315
return [
1416
'with' => 'array',
15-
'with.*' => 'string|required',
17+
'with.*' => 'string|required|in:' . $availableRelations,
1618
];
1719
}
1820

@@ -26,4 +28,12 @@ public function validateResolved(): void
2628
throw new NotFoundHttpException(__('validation.exceptions.not_found', ['entity' => 'Post']));
2729
}
2830
}
31+
32+
//TODO: don't forget to review relations list
33+
protected function getAvailableRelations(): array
34+
{
35+
return [
36+
'role',
37+
];
38+
}
2939
}

tests/fixtures/CommandTest/search_request.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class SearchPostsRequest extends Request
99
{
1010
public function rules(): array
1111
{
12+
$availableRelations = implode(',', $this->getAvailableRelations());
13+
1214
return [
1315
'page' => 'integer',
1416
'per_page' => 'integer',
@@ -17,7 +19,15 @@ public function rules(): array
1719
'all' => 'boolean',
1820
'with' => 'array',
1921
'query' => 'string|nullable',
20-
'with.*' => 'string',
22+
'with.*' => 'string|in:' . $availableRelations,
23+
];
24+
}
25+
26+
//TODO: don't forget to review relations list
27+
protected function getAvailableRelations(): array
28+
{
29+
return [
30+
'role',
2131
];
2232
}
2333
}

tests/fixtures/CommandTest/search_request_subfolder_model.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class SearchPostsRequest extends Request
99
{
1010
public function rules(): array
1111
{
12+
$availableRelations = implode(',', $this->getAvailableRelations());
13+
1214
return [
1315
'page' => 'integer',
1416
'per_page' => 'integer',
@@ -17,7 +19,15 @@ public function rules(): array
1719
'all' => 'boolean',
1820
'with' => 'array',
1921
'query' => 'string|nullable',
20-
'with.*' => 'string',
22+
'with.*' => 'string|in:' . $availableRelations,
23+
];
24+
}
25+
26+
//TODO: don't forget to review relations list
27+
protected function getAvailableRelations(): array
28+
{
29+
return [
30+
'role',
2131
];
2232
}
2333
}

tests/fixtures/RequestGeneratorTest/get_request.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ class GetPostRequest extends Request
1010
{
1111
public function rules(): array
1212
{
13+
$availableRelations = implode(',', $this->getAvailableRelations());
14+
1315
return [
1416
'with' => 'array',
15-
'with.*' => 'string|required',
17+
'with.*' => 'string|required|in:' . $availableRelations,
1618
];
1719
}
1820

@@ -26,4 +28,12 @@ public function validateResolved(): void
2628
throw new NotFoundHttpException(__('validation.exceptions.not_found', ['entity' => 'Post']));
2729
}
2830
}
31+
32+
//TODO: don't forget to review relations list
33+
protected function getAvailableRelations(): array
34+
{
35+
return [
36+
'role',
37+
];
38+
}
2939
}

tests/fixtures/RequestGeneratorTest/search_request.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class SearchPostsRequest extends Request
99
{
1010
public function rules(): array
1111
{
12+
$availableRelations = implode(',', $this->getAvailableRelations());
13+
1214
return [
1315
'user_id' => 'integer|exists:users,id|required',
1416
'page' => 'integer',
@@ -19,7 +21,15 @@ public function rules(): array
1921
'with' => 'array',
2022
'order_by' => 'string|in:' . $this->getOrderableFields(Post::class),
2123
'query' => 'string|nullable',
22-
'with.*' => 'string',
24+
'with.*' => 'string|in:' . $availableRelations,
25+
];
26+
}
27+
28+
//TODO: don't forget to review relations list
29+
protected function getAvailableRelations(): array
30+
{
31+
return [
32+
'role',
2333
];
2434
}
2535
}

0 commit comments

Comments
 (0)