diff --git a/src/Generators/RequestsGenerator.php b/src/Generators/RequestsGenerator.php index 504b222..c0b566d 100644 --- a/src/Generators/RequestsGenerator.php +++ b/src/Generators/RequestsGenerator.php @@ -76,6 +76,7 @@ protected function createRequest($method, $needToValidate = true, $parameters = 'namespace' => $this->getNamespace('requests'), 'servicesNamespace' => $this->getNamespace('services'), 'entityNamespace' => $this->getModelClass($this->model), + 'needToValidateWith' => !is_null(Arr::first($parameters, fn ($parameter) => $parameter['name'] === 'with.*')), ]); $this->saveClass('requests', "{$method}{$modelName}Request", @@ -195,7 +196,7 @@ protected function getRules($name, $type, $required, $nullable, $present): array $rules[] = 'present'; } - if ($name === 'order_by') { + if (in_array($name, ['order_by', 'with.*'])) { $rules[] = 'in:'; } diff --git a/stubs/request.blade.php b/stubs/request.blade.php index bb8fb4b..bde5819 100644 --- a/stubs/request.blade.php +++ b/stubs/request.blade.php @@ -16,9 +16,13 @@ class {{ $method }}{{ $entity }}Request extends Request public function rules(): array { @if(!empty($parameters)) +@if($needToValidateWith) + $availableRelations = implode(',', $this->getAvailableRelations()); + +@endif return [ @foreach($parameters as $parameter) - '{{ $parameter['name'] }}' => '{!! implode('|', $parameter['rules']) !!}'@if ($parameter['name'] === 'order_by') . $this->getOrderableFields({{ Str::singular($entity) }}::class)@endif, + '{{ $parameter['name'] }}' => '{!! implode('|', $parameter['rules']) !!}'@if ($parameter['name'] === 'order_by') . $this->getOrderableFields({{ Str::singular($entity) }}::class)@elseif($parameter['name'] === 'with.*'){{ ' . $availableRelations' }}@endif, @endforeach ]; @else @@ -41,4 +45,13 @@ public function validateResolved(): void } } @endif +@if($needToValidateWith) + + //TODO: don't forget to review relations list + protected function getAvailableRelations(): array + { + return [ + ]; + } +@endif } \ No newline at end of file diff --git a/tests/fixtures/CommandTest/get_request.php b/tests/fixtures/CommandTest/get_request.php index 4440a22..0f3e2bd 100644 --- a/tests/fixtures/CommandTest/get_request.php +++ b/tests/fixtures/CommandTest/get_request.php @@ -10,9 +10,11 @@ class GetPostRequest extends Request { public function rules(): array { + $availableRelations = implode(',', $this->getAvailableRelations()); + return [ 'with' => 'array', - 'with.*' => 'string|required', + 'with.*' => 'string|required|in:' . $availableRelations, ]; } @@ -26,4 +28,11 @@ public function validateResolved(): void throw new NotFoundHttpException(__('validation.exceptions.not_found', ['entity' => 'Post'])); } } + + //TODO: don't forget to review relations list + protected function getAvailableRelations(): array + { + return [ + ]; + } } \ No newline at end of file diff --git a/tests/fixtures/CommandTest/search_request.php b/tests/fixtures/CommandTest/search_request.php index 1009682..a6c6c80 100644 --- a/tests/fixtures/CommandTest/search_request.php +++ b/tests/fixtures/CommandTest/search_request.php @@ -9,6 +9,8 @@ class SearchPostsRequest extends Request { public function rules(): array { + $availableRelations = implode(',', $this->getAvailableRelations()); + return [ 'page' => 'integer', 'per_page' => 'integer', @@ -17,7 +19,14 @@ public function rules(): array 'all' => 'boolean', 'with' => 'array', 'query' => 'string|nullable', - 'with.*' => 'string', + 'with.*' => 'string|in:' . $availableRelations, + ]; + } + + //TODO: don't forget to review relations list + protected function getAvailableRelations(): array + { + return [ ]; } } \ No newline at end of file diff --git a/tests/fixtures/CommandTest/search_request_subfolder_model.php b/tests/fixtures/CommandTest/search_request_subfolder_model.php index 39db73e..ff7dc4e 100644 --- a/tests/fixtures/CommandTest/search_request_subfolder_model.php +++ b/tests/fixtures/CommandTest/search_request_subfolder_model.php @@ -9,6 +9,8 @@ class SearchPostsRequest extends Request { public function rules(): array { + $availableRelations = implode(',', $this->getAvailableRelations()); + return [ 'page' => 'integer', 'per_page' => 'integer', @@ -17,7 +19,14 @@ public function rules(): array 'all' => 'boolean', 'with' => 'array', 'query' => 'string|nullable', - 'with.*' => 'string', + 'with.*' => 'string|in:' . $availableRelations, + ]; + } + + //TODO: don't forget to review relations list + protected function getAvailableRelations(): array + { + return [ ]; } } \ No newline at end of file diff --git a/tests/fixtures/RequestGeneratorTest/get_request.php b/tests/fixtures/RequestGeneratorTest/get_request.php index 4440a22..0f3e2bd 100644 --- a/tests/fixtures/RequestGeneratorTest/get_request.php +++ b/tests/fixtures/RequestGeneratorTest/get_request.php @@ -10,9 +10,11 @@ class GetPostRequest extends Request { public function rules(): array { + $availableRelations = implode(',', $this->getAvailableRelations()); + return [ 'with' => 'array', - 'with.*' => 'string|required', + 'with.*' => 'string|required|in:' . $availableRelations, ]; } @@ -26,4 +28,11 @@ public function validateResolved(): void throw new NotFoundHttpException(__('validation.exceptions.not_found', ['entity' => 'Post'])); } } + + //TODO: don't forget to review relations list + protected function getAvailableRelations(): array + { + return [ + ]; + } } \ No newline at end of file diff --git a/tests/fixtures/RequestGeneratorTest/search_request.php b/tests/fixtures/RequestGeneratorTest/search_request.php index 580a4f1..7e76ce9 100644 --- a/tests/fixtures/RequestGeneratorTest/search_request.php +++ b/tests/fixtures/RequestGeneratorTest/search_request.php @@ -9,6 +9,8 @@ class SearchPostsRequest extends Request { public function rules(): array { + $availableRelations = implode(',', $this->getAvailableRelations()); + return [ 'user_id' => 'integer|exists:users,id|required', 'page' => 'integer', @@ -19,7 +21,14 @@ public function rules(): array 'with' => 'array', 'order_by' => 'string|in:' . $this->getOrderableFields(Post::class), 'query' => 'string|nullable', - 'with.*' => 'string', + 'with.*' => 'string|in:' . $availableRelations, + ]; + } + + //TODO: don't forget to review relations list + protected function getAvailableRelations(): array + { + return [ ]; } } \ No newline at end of file