Enforce request validation style rules
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\Project;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class DeleteProjectRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return $this->user() !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
/** @var Project $project */
|
||||
$project = $this->route('project');
|
||||
|
||||
return [
|
||||
'confirmation_key' => [
|
||||
'required',
|
||||
'string',
|
||||
Rule::in([$project->key]),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
'confirmation_key.in' => 'Enter the exact project key to confirm deletion.',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class StoreProjectRequest extends FormRequest
|
||||
@@ -18,9 +19,11 @@ class StoreProjectRequest extends FormRequest
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
if ($this->has('key')) {
|
||||
$key = Arr::get($this->all(), 'key');
|
||||
|
||||
if ($key !== null) {
|
||||
$this->merge([
|
||||
'key' => strtoupper((string) $this->string('key')->trim()),
|
||||
'key' => strtoupper(trim((string) $key)),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,14 +71,16 @@ class UpdateProjectTicketRequest extends FormRequest
|
||||
{
|
||||
return [
|
||||
function (Validator $validator): void {
|
||||
/** @var array<string, mixed> $validated */
|
||||
$validated = $this->validated();
|
||||
|
||||
/** @var Project $project */
|
||||
$project = $this->route('project');
|
||||
|
||||
/** @var Ticket $ticket */
|
||||
$ticket = $this->route('ticket');
|
||||
|
||||
$parentTicketId = $this->input('parent_ticket_id');
|
||||
$parentTicketId = is_numeric($parentTicketId) ? (int) $parentTicketId : null;
|
||||
$parentTicketId = Arr::get($validated, 'parent_ticket_id');
|
||||
|
||||
if ($parentTicketId !== null) {
|
||||
if ($parentTicketId === $ticket->id) {
|
||||
@@ -105,12 +107,10 @@ class UpdateProjectTicketRequest extends FormRequest
|
||||
}
|
||||
}
|
||||
|
||||
$requestedTicketTypeId = $this->input('ticket_type_id');
|
||||
$requestedTicketType = is_numeric($requestedTicketTypeId)
|
||||
? TicketType::query()
|
||||
->where('project_id', $project->id)
|
||||
->find((int) $requestedTicketTypeId)
|
||||
: null;
|
||||
$requestedTicketTypeId = Arr::get($validated, 'ticket_type_id');
|
||||
$requestedTicketType = TicketType::query()
|
||||
->where('project_id', $project->id)
|
||||
->find($requestedTicketTypeId);
|
||||
|
||||
if (
|
||||
$requestedTicketType?->system_key !== 'parent'
|
||||
@@ -119,61 +119,13 @@ class UpdateProjectTicketRequest extends FormRequest
|
||||
$validator->errors()->add('ticket_type_id', 'Tickets with child tickets must keep a Parent type.');
|
||||
}
|
||||
|
||||
if ($this->boolean('is_blocked') && blank($this->input('blocked_comment'))) {
|
||||
if (Arr::get($validated, 'is_blocked', false) && blank(Arr::get($validated, 'blocked_comment'))) {
|
||||
$validator->errors()->add('blocked_comment', 'Blocked tickets require a blocker comment.');
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{
|
||||
* acceptance_criteria: string|null,
|
||||
* blocked_comment: string|null,
|
||||
* description: string|null,
|
||||
* is_blocked: bool,
|
||||
* parent_ticket_id: int|null,
|
||||
* ticket_priority_id: int,
|
||||
* ticket_type_id: int,
|
||||
* title: string
|
||||
* }
|
||||
*/
|
||||
public function ticketAttributes(): array
|
||||
{
|
||||
/** @var array{
|
||||
* acceptance_criteria: string|null,
|
||||
* blocked_comment: string|null,
|
||||
* description: string|null,
|
||||
* is_blocked: bool,
|
||||
* parent_ticket_id: int|null,
|
||||
* ticket_priority_id: int,
|
||||
* ticket_type_id: int,
|
||||
* title: string
|
||||
* } $attributes
|
||||
*/
|
||||
$attributes = array_merge(
|
||||
[
|
||||
'acceptance_criteria' => null,
|
||||
'blocked_comment' => null,
|
||||
'description' => null,
|
||||
'is_blocked' => false,
|
||||
'parent_ticket_id' => null,
|
||||
],
|
||||
Arr::only($this->validated(), [
|
||||
'acceptance_criteria',
|
||||
'blocked_comment',
|
||||
'description',
|
||||
'is_blocked',
|
||||
'parent_ticket_id',
|
||||
'ticket_priority_id',
|
||||
'ticket_type_id',
|
||||
'title',
|
||||
]),
|
||||
);
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
private function createsCycle(Ticket $ticket, Ticket $candidateParent): bool
|
||||
{
|
||||
$currentParent = $candidateParent;
|
||||
|
||||
Reference in New Issue
Block a user