Why this lesson matters
Understand api validation errors and apply it in a small Laravel feature. A Laravel API is a public contract: routes define the surface, validation protects input, resources shape output, and status/error behavior lets clients integrate predictably.
How to reason about it
- For API Validation Errors, the outcome to verify is: Keep validation close to the boundary where data enters the system.
- In API Validation Errors, keep this failure controlled: Returning raw model attributes or exception messages creates a brittle API and can leak fields that were never meant to be public.
- API Validation Errors practice target: Create one JSON endpoint with validation and a Resource, then test success and validation-error responses.
Practical walkthrough
In the API Validation Errors walkthrough: Keep validation close to the boundary where data enters the system.
app/Http/Requests/StoreUserRequest.phpphp
public function rules(): array
{
return [
'name' => ['required', 'string', 'max:120'],
'email' => ['required', 'email'],
];
}Practice it yourself
API Validation Errors exercise
Create one JSON endpoint with validation and a Resource, then test success and validation-error responses.
- Record the expected result before execution
- Test one valid path and one lesson-specific failure path
- Explain in two lines which boundary owns the decision
