Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BCrwHHnGCB5XH968Nxokqw
57 lines
1.5 KiB
PHP
57 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Ingest\EventRules;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Validator;
|
|
|
|
class UpsertEventsRequest extends FormRequest
|
|
{
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'events' => ['required', 'array', 'min:1', 'max:500'],
|
|
'events.*.user' => ['required', 'string', 'max:255'],
|
|
'events.*.source_ref' => ['required', 'string', 'max:255'],
|
|
'events.*.topic' => ['sometimes', 'nullable', 'string', 'max:255'],
|
|
] + EventRules::itemPrefixed('events.*.');
|
|
}
|
|
|
|
public function messages(): array
|
|
{
|
|
return EventRules::messages('events.*.');
|
|
}
|
|
|
|
public function after(): array
|
|
{
|
|
return [function (Validator $v) {
|
|
foreach ((array) $this->input('events', []) as $i => $item) {
|
|
if (! is_array($item)) {
|
|
continue;
|
|
}
|
|
foreach (EventRules::check($item) as $field => $error) {
|
|
if ($v->errors()->missing("events.$i.$field")) {
|
|
$v->errors()->add("events.$i.$field", $error);
|
|
}
|
|
}
|
|
}
|
|
}];
|
|
}
|
|
|
|
/** @return list<array<string,mixed>> */
|
|
public function items(): array
|
|
{
|
|
return array_map(function (array $item) {
|
|
$item['topic'] = (string) ($item['topic'] ?? '');
|
|
|
|
return $item;
|
|
}, $this->validated()['events']);
|
|
}
|
|
}
|