Skip to content

Add array items validation and wildcard support. #1340

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

Closed
wants to merge 7 commits into from
Closed

Add array items validation and wildcard support. #1340

wants to merge 7 commits into from

Conversation

KaneCohen
Copy link
Contributor

Ok, i know that you've asked me to stop making PRs on validation, but after a lot of thought i came to the conclusion that it's impossible to make Validator intelligent enough so that it'll know when and how to validate an array and its contents.

Simple example. Validate following array against these rules:

  • Make sure that it's an array
  • It has at least 3 items
  • Each item is a string and has at least 3 character in it
  • Check each item with exists

I think it's just not possible unless you have at least two attributes and some serious magic in Validator going on. This PR tries to make it easy to use and to understand for a developer without serious docs crunching or voodoo magic.

$input = array('input' => array('foo', 'bar', 'baz'));
$rules = array(
    'input'   => 'Array|Min:3|Exists:users,name', // Run Exists against whole array - only one query
    'input.*' => 'String|Min:3'
);

$v = Validator::make($input, $rules);

First rule makes sure our input attribute is an array with at least three items. Check.
Second rules uses array_select and wildcrads to check each item of the array if it's a string and if it has at least 3 characters. Check.

I think it's pretty simple and intuitive.

Based upon my two other PRs: #1134 and #833.

@KaneCohen
Copy link
Contributor Author

Hmm, i wonder why Travis CI won't test this PR.

@Ruby184
Copy link
Contributor

Ruby184 commented May 17, 2013

👍 Good job! Personally, I think it is very good solution for array validation.

@KaneCohen
Copy link
Contributor Author

Rewrite to handle certain edge cases while using wildcards. Cases like so:

$input = array(
    'criminals' => array(
       0 => array(
            'name' =>  'Mike Masters',
            'aliases' => ['Big Mac', 'Mikey']
        ),
       1 => array(
            'name' =>  'Sam Floyd',
            'aliases' => ['SS', 'Pinky']
        )
    )
);

$rules = array(
    'criminals.*.name'  => 'Exists:users,name', //  ['Mike Masters', 'Sam Floyd']
    'criminals.*.aliases' => 'Array|Max:3', // ['Big Mac', 'Mikey'], ['SS', 'Pinky']
    'criminals.*.aliases.*' => 'Exists:aliases,name' // ['Big Mac', 'Mikey', 'SS', 'Pinky']
);

$v = Validator::make($input, $rules);

Before last commit Exists would have been applied to every criminal's name and alias (one query per criminal for names and one query per alias - 6 total). With this rewrite Validator will make only two queries. One for names and one for aliases.

@schickling
Copy link

Nice! I implemented something like this already on my own. I'd love to see this (or something similar) baked in laravel 👍

@KaneCohen
Copy link
Contributor Author

Moved array_select back to array_get name. I really think framework would be better off with native support for wildcards without extra function.

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.

3 participants