DEV Community

Cover image for 🚀 Laravel 13.24 Added a Small Rule That Solves a Really Common API Headache
devTalk
devTalk

Posted on

🚀 Laravel 13.24 Added a Small Rule That Solves a Really Common API Headache

Ever had a client send extra, unexpected keys inside a filter or settings array? A typo, stale frontend field, or someone poking at your validation?

Laravel finally has a clean, built-in way to lock this down: Rule::arrayKeys().

'filter' => Rule::arrayKeys([
'status',
'author',
'tag',
]),

Send anything outside status, author, or tag, and validation fails — with a message that actually tells you what went wrong (not just a vague "must be an array").

In the article I cover:
🔹 How array_keys differs from the old array:key1,key2 syntax (and why the old error messages were misleading)
🔹 Pairing it with required_array_keys() when some keys must be present
🔹 Using the :unexpected placeholder for genuinely helpful error responses
🔹 A full real-world example validating an API filter payload

No more writing custom Rule classes just to check array shapes.

Full writeup + code here: Link

What's your go-to approach for validating nested array payloads in Laravel? Curious if anyone's been doing this a different way.

Top comments (0)