Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Inferences for Laravel Validation + PHPStan

phpstan-laravel-validation

Laravel validation is not a typed data boundary. This extension recovers sound structural types for successful validated() output from statically resolvable rule sets.

The inferred type may be broader than a rule name suggests because Laravel usually preserves the original native value. See Laravel Validation and Type Safety for the evidence.

Caution

For new type-conscious code, prefer a boundary with an explicit, normalized output contract. This library is a mitigation for existing Laravel validation, not an endorsement of that design.

Start here

$data = \Illuminate\Support\Facades\Validator::make($request->all(), [
    'person' => 'required|array',
    'person.*.email' => 'required|string|email|unique:users',
    'person.*.first_name' => 'required|string',
    'person.*.age' => 'required|integer|string',
])->validated();

\PHPStan\dumpType($data);
// array{person: array<int|string, array{
//   email: non-empty-string,
//   first_name: string,
//   age: numeric-string
// }>}

The 0.1 line is an experimental public release. Compatibility and known limitations are in Getting Started and Limitations.