Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Generators/RequestsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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:';
}

Expand Down
15 changes: 14 additions & 1 deletion stubs/request.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
11 changes: 10 additions & 1 deletion tests/fixtures/CommandTest/get_request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}

Expand All @@ -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 [
];
}
}
11 changes: 10 additions & 1 deletion tests/fixtures/CommandTest/search_request.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class SearchPostsRequest extends Request
{
public function rules(): array
{
$availableRelations = implode(',', $this->getAvailableRelations());

return [
'page' => 'integer',
'per_page' => 'integer',
Expand All @@ -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 [
];
}
}
11 changes: 10 additions & 1 deletion tests/fixtures/CommandTest/search_request_subfolder_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class SearchPostsRequest extends Request
{
public function rules(): array
{
$availableRelations = implode(',', $this->getAvailableRelations());

return [
'page' => 'integer',
'per_page' => 'integer',
Expand All @@ -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 [
];
}
}
11 changes: 10 additions & 1 deletion tests/fixtures/RequestGeneratorTest/get_request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}

Expand All @@ -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 [
];
}
}
11 changes: 10 additions & 1 deletion tests/fixtures/RequestGeneratorTest/search_request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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 [
];
}
}
Loading