Skip to content

[13.x] Add an array_keys validation rule - #60918

Merged
taylorotwell merged 1 commit into
laravel:13.xfrom
nebarg:array-keys-validation-rule
Jul 29, 2026
Merged

[13.x] Add an array_keys validation rule#60918
taylorotwell merged 1 commit into
laravel:13.xfrom
nebarg:array-keys-validation-rule

Conversation

@nebarg

@nebarg nebarg commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds an array_keys validation rule for the common case of "this array may only contain these keys, anything else is invalid".

$request->validate([
    'options' => Rule::arrayKeys(['sort', 'direction']),
]);

// or as a string
$request->validate([
    'options' => 'array_keys:sort,direction',
]);

Given ['sort' => 'name', 'colour' => 'red'], this fails with:

The options field must only contain the following keys: sort, direction.

Keys are optional, not required; the rule constrains which keys may appear, it does not demand they all be present. (required_array_keys already covers this)

Why not just array:key_1,key_2?

Rule::array() already accepts keys and does reject unexpected ones, but it conflates two different failures under one message. Validating ['key_1' => 'bar', 'colour' => 'red']:

Rule Message
array:key_1,key_2 The foo field must be an array.
array_keys:key_1,key_2 The foo field must only contain the following keys: key_1, key_2.

The array rule answers "is this an array?" and "does it have only these keys?" with the same message, it cannot say which one failed, and there is no placeholder for the offending keys.

$validator->failed() reports ArrayKeys rather than Array which lets the two be composed where you want both checks reported independently:

'options' => ['array', Rule::arrayKeys(['sort', 'direction'])],

This PR leaves Rule::array() and its message completely untouched.

Custom messages

Two placeholders are available:

  • :values - the accepted keys (used by the default message)
  • :unexpected - the offending keys that caused the failure

:unexpected is not in the default message, but it is available to anyone writing their own, which is often the more useful half for an API response:

$request->validate(
    ['options' => Rule::arrayKeys(['sort', 'direction'])],
    ['options.array_keys' => 'The :attribute field may not contain :unexpected.'],
);

// The options field may not contain colour.

Details

  • Accepts an array, Arrayable etc, matching Rule::array().
  • Requires at least one key, array_keys with no parameters throws.
  • A non-array value fails; pair with array if you want the type failure reported separately.

Backwards compatibility

This is a new rule, builder, and array_keys message line. No existing rule, message, or validation behaviour is modified.

Adds Rule::arrayKeys() and the "array_keys:foo,bar" rule string, failing
when an array contains keys beyond the accepted set. The accepted keys
are available in the message as :values, and the keys that caused the
failure as :unexpected.
@taylorotwell
taylorotwell merged commit 91eee4b into laravel:13.x Jul 29, 2026
54 of 55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants