Skip to content

Omit redundant parent ID validation #732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 9 additions & 5 deletions src/Generators/Statements/FormRequestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,24 @@ protected function populateStub(string $stub, string $name, $context, ValidateSt
{
$stub = str_replace('{{ namespace }}', config('blueprint.namespace') . '\\Http\\Requests' . ($controller->namespace() ? '\\' . $controller->namespace() : ''), $stub);
$stub = str_replace('{{ class }}', $name, $stub);
$stub = str_replace('{{ rules }}', $this->buildRules($context, $validateStatement), $stub);
$stub = str_replace('{{ rules }}', $this->buildRules($context, $validateStatement, $controller), $stub);

return $stub;
}

protected function buildRules(string $context, ValidateStatement $validateStatement): string
protected function buildRules(string $context, ValidateStatement $validateStatement, Controller $controller): string
{
return trim(
array_reduce(
$validateStatement->data(),
function ($output, $field) use ($context) {
function ($output, $field) use ($context, $controller) {
[$qualifier, $column] = $this->splitField($field);

if (is_null($qualifier)) {
$qualifier = $context;
}

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

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

protected function validationRules(string $qualifier, string $column): array
protected function validationRules(string $qualifier, string $column, Controller $controller): array
{
/**
* @var \Blueprint\Models\Model $model
Expand All @@ -129,6 +129,10 @@ protected function validationRules(string $qualifier, string $column): array
continue;
}

if ($column->name() === Str::snake($controller->parent()) . '_id') {
continue;
}

$rules[$column->name()] = Rules::fromColumn($model->tableName(), $column);
}

Expand Down
23 changes: 23 additions & 0 deletions tests/Feature/Generators/Statements/FormRequestGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,27 @@ public function test_output_generates_form_request_without_softdeletestz(): void
],
], $this->subject->output($tree));
}

public function test_output_generates_form_request_without_parent_id_column_validation(): void
{
$this->filesystem->expects('stub')
->with('request.stub')
->andReturn($this->stub('request.stub'));
$this->filesystem->expects('exists')
->twice()
->with('app/Http/Requests')
->andReturnFalse();
$this->filesystem->expects('put')
->with('app/Http/Requests/CommentStoreRequest.php', $this->fixture('form-requests/form-requests-controller-has-parent.php'));

$tokens = $this->blueprint->parse($this->fixture('drafts/form-requests-controller-has-parent.yaml'));
$tree = $this->blueprint->analyze($tokens);

self::assertSame([
'created' => [
'app/Http/Requests/CommentStoreRequest.php',
'app/Http/Requests/CommentUpdateRequest.php',
],
], $this->subject->output($tree));
}
}
15 changes: 15 additions & 0 deletions tests/fixtures/drafts/form-requests-controller-has-parent.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
models:
Post:
title: string
body: text
relationships:
hasMany: comment
Comment:
body: text
relationships:
belongsTo: post
controllers:
Comment:
resource: api
meta:
parent: post
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CommentStoreRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'body' => ['required', 'string'],
];
}
}
Loading