mikeerickson / validatorjs

A data validation library in JavaScript for the browser and Node.js, inspired by Laravel's Validator.
https://www.npmjs.com/package/validatorjs
MIT License
1.77k stars 280 forks source link

Add support for wildcard rules definition to validate array elements #101

Open LKay opened 8 years ago

LKay commented 8 years ago

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).

iamdtang commented 8 years ago

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

LKay commented 8 years ago

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.

iamdtang commented 8 years ago

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?

LKay commented 8 years ago

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?

garygreen commented 8 years ago

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.

LKay commented 8 years ago

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?

garygreen commented 8 years ago

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

iamdtang commented 8 years ago

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

bricox commented 8 years ago

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.

iamdtang commented 8 years ago

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

bricox commented 8 years ago

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.

iamdtang commented 8 years ago

awesome, thanks!

andresilvagomez commented 8 years ago

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

vedmant commented 7 years ago

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

iamdtang commented 7 years ago

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

mikeerickson commented 7 years ago

@skaterdav85 Sure thing, I will tackle it next

mikeerickson commented 7 years ago

@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.

bricox commented 7 years ago

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.