validatorjs icon indicating copy to clipboard operation
validatorjs copied to clipboard

Add support for wildcard rules definition to validate array elements

Open LKay opened this issue 9 years ago • 18 comments

It would be nice to have array rule improved, so it would take an optional parameter containing rue or set of rules to validate each element in the array accordingly. Probably the idea of composed rules mentioned in #100 or wrapping nested ruleset in parenthesis might be of use here to not mess with nested ruleset ie. required|array:(string|min:3).

LKay avatar Jul 06 '16 01:07 LKay

Would it just check the type of each element in the array, so you could see if the array contained strings, objects, etc?

iamdtang avatar Jul 06 '16 01:07 iamdtang

Basically yes. The idea is to be able to validate array elements.

Use case: I want to pass an array which contains strings with length of at least 3 characters.

LKay avatar Jul 06 '16 02:07 LKay

Seems like this would be better as a custom rule since it seems very specific. Plus, if we did add it, to what degree would it be? Validating the array's element types? Validating the structure or contents of the array's elements? Does Laravel do something like this? What do you think @garygreen?

iamdtang avatar Jul 06 '16 05:07 iamdtang

The idea is to validate just array contents and not structure. I imagine that would iterate over array elements and run validation for each.

Laravel doesn't do that and requires to write validation for array manually according to docs. Maybe it's indeed a better idea to create another rule and leave array as it is. Yet I think it's pretty common case when you want to validate array contents.

What do you think about having a core validatorjs containing the same set of rules ad Laravel and separate plugin-like package with additional rules that Laravel doesn't have?

LKay avatar Jul 06 '16 05:07 LKay

Laravel doesn't do that and requires to write validation for array manually according to docs.

Laravel does have array wildcard validation, older versions of Laravel used the each rule/function. I think we should support wildcards.

garygreen avatar Jul 06 '16 08:07 garygreen

Supporting wild cards for array sounds like a better solution and it would be the same as Laravel handling such cases. For that purpose maybe we also should abandon support for defining rules as nested object and just support flat structure to be more compliant with original library?

LKay avatar Jul 07 '16 17:07 LKay

I don't think Laravel supports nested rules? Would make more sense to support wildcards instead.

garygreen avatar Jul 07 '16 18:07 garygreen

Im looking at the Laravel Validator docs for the array rule and I don't see anything about wildcards. Is it located somewhere else?

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

iamdtang avatar Jul 07 '16 19:07 iamdtang

I'm looking at this documentation.
https://laravel.com/docs/5.2/validation#validating-arrays

Note the use of wild cards in the email and first_name properties.

bricox avatar Aug 15 '16 19:08 bricox

Ah I see it. I don't have time to add this, but if someone wants to add this feature, please do!

iamdtang avatar Aug 16 '16 04:08 iamdtang

I implemented it last night. I have a little more testing to do and then I'll send a pull request. I also have a few updated to the readme I want to send.

bricox avatar Aug 16 '16 17:08 bricox

awesome, thanks!

iamdtang avatar Aug 16 '16 17:08 iamdtang

@bricox @skaterdav85 Hi i read that, is amazing, is now implemented? or yet help with that? thanks !!

andresilvagomez avatar Sep 14 '16 21:09 andresilvagomez

Hi guys, any update for validating arrays? I'd use this feature in my project.

vedmant avatar Mar 11 '17 09:03 vedmant

@bricox you still working on this? If not, @mikeerickson, want to help out with this?

iamdtang avatar Mar 11 '17 17:03 iamdtang

@skaterdav85 Sure thing, I will tackle it next

mikeerickson avatar Mar 11 '17 19:03 mikeerickson

@bricox Seeing as you have already implemented, no sense in duplicating efforts. Unless of course you are not prepared to make a PR, at which I will work on adding it.

mikeerickson avatar Mar 11 '17 20:03 mikeerickson

Ah, sorry I dropped this guys. Unfortunately I never finished the testing. If you still want it I can make it available, but I assuming you have implemented by now.

bricox avatar Apr 08 '17 11:04 bricox