[13.x] Add an array_keys validation rule - #60918
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds an
array_keysvalidation rule for the common case of "this array may only contain these keys, anything else is invalid".Given
['sort' => 'name', 'colour' => 'red'], this fails with:Keys are optional, not required; the rule constrains which keys may appear, it does not demand they all be present. (
required_array_keysalready 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']:array:key_1,key_2array_keys:key_1,key_2The
arrayrule 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()reportsArrayKeysrather thanArraywhich lets the two be composed where you want both checks reported independently: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:unexpectedis 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:Details
Arrayableetc, matchingRule::array().array_keyswith no parameters throws.arrayif you want the type failure reported separately.Backwards compatibility
This is a new rule, builder, and
array_keysmessage line. No existing rule, message, or validation behaviour is modified.