← BACK
Laravel / Validation

Restrict Array Keys with the array_keys Validation Rule in Laravel 13.24

Punyapal Shah • • 1 min read
edit this tip →

Laravel 13.24 adds the array_keys validation rule to reject any unexpected keys in an array input, keeping input strictly limited to allowed keys.

When building API endpoints that receive settings or configuration objects, unexpected extra keys could indicate tampering or version mismatches. The array_keys rule restricts input to allowed keys only.

use Illuminate\Support\Facades\Validator;

$validator = Validator::make($request->all(), [
    'settings'   => 'required|array|array_keys:theme,language,timezone',
    'settings.*' => 'string',
]);
  • array_keys: rejects keys NOT in your list (whitelist)
  • required_array_keys: checks that specific keys ARE present (required check)
  • Rejects payloads containing unlisted keys

Related Tips

View all tips →
Laravel Validation

Optimize Large Wildcard Array Validation in Laravel 13.24

Laravel 13.24 introduces dramatic performance optimizations for validating large nested arrays with wildcard rules, reducing validation execution time from 85 seconds to under 1 second.

READ >

// Got a tip in mind? Contributions are always welcome