[13.x] Add a dedicated validation message for array key failures#60885
Open
nebarg wants to merge 1 commit into
Open
[13.x] Add a dedicated validation message for array key failures#60885nebarg wants to merge 1 commit into
nebarg wants to merge 1 commit into
Conversation
When the "array" rule is given a set of accepted keys and rejects a genuine array because of an unexpected key, report it with a specific "array_keys" message that lists the accepted (:values) and unexpected (:unexpected) keys, instead of the misleading "must be an array" message. The failure is still recorded as the "array" rule, so failed() is unchanged.
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
The validator
arrayrule accepts an optional set of allowed keys such asRule::array(['key_1', 'key_2']), or the string formarray:key_1,key_2. When the posted value is an array but contains a key outside the given acceptable list, it currently fails with a generic message:This is misleading; the value is an array. This PR gives that specific failure its own
array_keysmessage that names both the allowed and the offending keys:Details
Rule::array([...])and the stringarray:key_1,key_2form (they resolve to the same rule).array_keyskey (such asfoo.array_keys), exposing:values(allowed keys) and:unexpected(offending keys); either can be used on its own or both omitted entirely.Backwards compatibility
The new message replaces the old one only for the case it describes, an array containing unexpected keys. Every other array failure keeps the existing message:
arrayrule used without keys still fails with "must be an array".$validator->failed()continues to record the failure under thearrayrule, so nothing that inspects the failed rules changes. The only behavioural change is the wording of that one failure case. Apps that have published language files should add the newarray_keysline, and any custom message for this case can move to thearray_keyskey.