laravel-validation
Installation
SKILL.md
Laravel Validation
Every input an endpoint accepts is validated by a Form Request, and the Form Request does nothing else. It doesn't authorize: the route does. It doesn't map: the code that builds the DTO reads the validated request. Each Form Request is pinned by a dataset-driven test that sends a broken request to the real endpoint, one rule per case, and asserts the exact message.
Rules
- A Form Request validates every input. Never
$request->validate()orValidator::make()in a controller. laravel-data's validation is switched off, so the Form Request is the only validation that runs (see laravel-dtos). - One request per surface and operation.
App\Http\{Surface}\Requests\{Verb}{Entity}Request, for exampleApp\Http\Api\V1\Requests\CreateOrderRequest. When two surfaces take the same input, each gets its own request, so one surface's contract can change without touching the other's. - A request only validates. No
authorize(): authorization is->can()on the route (see laravel-routing). NotoDto(),prepareForValidation()orpassedValidation(): mapping and casting happen where the DTO is built, through typed request helpers. - Array rules, one per line. Never a pipe string. Each rule gets its own line, so a diff shows exactly which rule changed and a rule object sits alongside the strings.
- Order: presence, type, bounds, then the expensive checks.
requiredornullablefirst, then the type (string,integer,array), then size and format, then database lookups and rule objects. bailsits just before the rule it protects. It applies to the whole attribute wherever it appears; Laravel treats it as a flag, not a position. Placing it before the lookup or rule object tells the reader what the type checks guard, and that check never runs on input that already failed.- Enums use
new Enum(OrderPriority::class), after'string'and'bail'. Never anin:list of case values: the list goes stale when a case is added. - Name fields in
attributes(), notmessages(). Laravel builds every message from the attribute's name, so mappingitems.*.product_ulidtoproductfixes all of that field's messages at once. Usemessages()only for wording the attribute name can't produce. A rule object writes its own message.
Example
<?php