framework icon indicating copy to clipboard operation
framework copied to clipboard

[11.x] Introduce method `Rule::array()`

Open Jacobs63 opened this issue 1 year ago • 0 comments

This PR introduces a new array method to \Illuminate\Validation\Rule.

Currently, it is often cumbersome to validate multiple array keys through the array rule. At the same time, if intending to using constants, or enums, we may end up in defining our rule such as:

['array:' . MyBackedEnum::VALUE->value . ',' . MyBackedEnum::VALUE_2->value]

The goal of this change is to improve code readability and prevent uncaught typos when validating array keys, but to also improve the existing functionalities by introducing support for Arrayable & lists of enums.

The newly introduced ArrayRule works very similar to the already existing In rule, by stringifying to the original array string rule, introducing no breaking changes.

In summary, the following is now possible:

Rule::array();

Rule::array('key_1', 'key_2', 'key_3');

Rule::array(['key_1', 'key_2', 'key_3']);

Rule::array(collect(['key_1', 'key_2', 'key_3']));

Rule::array([UnitEnum::key_1, UnitEnum::key_2, UnitEnum::key_3]);

Rule::array([BackedEnum::key_1, BackedEnum::key_2, BackedEnum::key_3]);

Jacobs63 avatar Apr 30 '24 09:04 Jacobs63