Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
May 3, 2024
Merged

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

merged 4 commits into from
May 3, 2024

Conversation

Jacobs63
Copy link
Contributor

@Jacobs63 Jacobs63 commented Apr 30, 2024

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]);

@vaishnavyogesh
Copy link

@Jacobs63 why not use Rule::enum() for matching against an enum's cases?

@Jacobs63
Copy link
Contributor Author

Jacobs63 commented May 1, 2024

@Jacobs63 why not use Rule::enum() for matching against an enum's cases?

The array rule does not serve for validating values, it can either validate, that:

  1. The input value is of type array
  2. The input value is of type array and only contains specific keys

Unlike enum rule, which validates values and can validate that:

  1. The input value matches the enum name (if UnitEnum)
  2. The input value matches the enum value (if BackedEnum)

They're two different validation rules.

@vaishnavyogesh
Copy link

@Jacobs63 why not use Rule::enum() for matching against an enum's cases?

The array rule does not serve for validating values, it can either validate, that:

  1. The input value is of type array
  2. The input value is of type array and only contains specific keys

Unlike enum rule, which validates values and can validate that:

  1. The input value matches the enum name (if UnitEnum)
  2. The input value matches the enum value (if BackedEnum)

They're two different validation rules.

My bad! I misunderstood the usecase, thanks for the explaination though.

@taylorotwell taylorotwell merged commit 8c684a2 into laravel:11.x May 3, 2024
28 checks passed
@osbre
Copy link
Contributor

osbre commented May 4, 2024

What's the difference between Rule::in and Rule::array? Why another method?

@decadence
Copy link
Contributor

What's the difference between Rule::in and Rule::array? Why another method?

array defines keys that validated value (array) can have. in is list of allowed values for validated value.

@osbre
Copy link
Contributor

osbre commented May 4, 2024

@decadence Thank you. I wasn't aware there's already an array rule in the first place 😅

https://laravel.com/docs/11.x/validation#rule-array

func0der pushed a commit to func0der/framework that referenced this pull request May 6, 2024
* [11.x] Introduce method `Rule::array()`

* CS fixes

* Add `func_get_args` test

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
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.

5 participants