Skip to content

Commit a748d8a

Browse files
authored
Omit redundant parent ID validation (#732)
1 parent 3e4fffc commit a748d8a

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

src/Generators/Statements/FormRequestGenerator.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,24 @@ protected function populateStub(string $stub, string $name, $context, ValidateSt
6363
{
6464
$stub = str_replace('{{ namespace }}', config('blueprint.namespace') . '\\Http\\Requests' . ($controller->namespace() ? '\\' . $controller->namespace() : ''), $stub);
6565
$stub = str_replace('{{ class }}', $name, $stub);
66-
$stub = str_replace('{{ rules }}', $this->buildRules($context, $validateStatement), $stub);
66+
$stub = str_replace('{{ rules }}', $this->buildRules($context, $validateStatement, $controller), $stub);
6767

6868
return $stub;
6969
}
7070

71-
protected function buildRules(string $context, ValidateStatement $validateStatement): string
71+
protected function buildRules(string $context, ValidateStatement $validateStatement, Controller $controller): string
7272
{
7373
return trim(
7474
array_reduce(
7575
$validateStatement->data(),
76-
function ($output, $field) use ($context) {
76+
function ($output, $field) use ($context, $controller) {
7777
[$qualifier, $column] = $this->splitField($field);
7878

7979
if (is_null($qualifier)) {
8080
$qualifier = $context;
8181
}
8282

83-
$validationRules = $this->validationRules($qualifier, $column);
83+
$validationRules = $this->validationRules($qualifier, $column, $controller);
8484

8585
foreach ($validationRules as $name => $rule) {
8686
$formattedRule = implode("', '", $rule);
@@ -104,7 +104,7 @@ private function splitField($field): array
104104
return [null, $field];
105105
}
106106

107-
protected function validationRules(string $qualifier, string $column): array
107+
protected function validationRules(string $qualifier, string $column, Controller $controller): array
108108
{
109109
/**
110110
* @var \Blueprint\Models\Model $model
@@ -129,6 +129,10 @@ protected function validationRules(string $qualifier, string $column): array
129129
continue;
130130
}
131131

132+
if ($column->name() === Str::snake($controller->parent()) . '_id') {
133+
continue;
134+
}
135+
132136
$rules[$column->name()] = Rules::fromColumn($model->tableName(), $column);
133137
}
134138

tests/Feature/Generators/Statements/FormRequestGeneratorTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,27 @@ public function test_output_generates_form_request_without_softdeletestz(): void
299299
],
300300
], $this->subject->output($tree));
301301
}
302+
303+
public function test_output_generates_form_request_without_parent_id_column_validation(): void
304+
{
305+
$this->filesystem->expects('stub')
306+
->with('request.stub')
307+
->andReturn($this->stub('request.stub'));
308+
$this->filesystem->expects('exists')
309+
->twice()
310+
->with('app/Http/Requests')
311+
->andReturnFalse();
312+
$this->filesystem->expects('put')
313+
->with('app/Http/Requests/CommentStoreRequest.php', $this->fixture('form-requests/form-requests-controller-has-parent.php'));
314+
315+
$tokens = $this->blueprint->parse($this->fixture('drafts/form-requests-controller-has-parent.yaml'));
316+
$tree = $this->blueprint->analyze($tokens);
317+
318+
self::assertSame([
319+
'created' => [
320+
'app/Http/Requests/CommentStoreRequest.php',
321+
'app/Http/Requests/CommentUpdateRequest.php',
322+
],
323+
], $this->subject->output($tree));
324+
}
302325
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
models:
2+
Post:
3+
title: string
4+
body: text
5+
relationships:
6+
hasMany: comment
7+
Comment:
8+
body: text
9+
relationships:
10+
belongsTo: post
11+
controllers:
12+
Comment:
13+
resource: api
14+
meta:
15+
parent: post
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class CommentStoreRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*/
12+
public function authorize(): bool
13+
{
14+
return true;
15+
}
16+
17+
/**
18+
* Get the validation rules that apply to the request.
19+
*/
20+
public function rules(): array
21+
{
22+
return [
23+
'body' => ['required', 'string'],
24+
];
25+
}
26+
}

0 commit comments

Comments
 (0)