Skip to content

Array validation #741

Closed
Closed
@KaneCohen

Description

@KaneCohen

Ok, i thought i'll make an issue related to Validator and its ability to validate arrays since i'm stuck right now thinking about best way to handle this problem.

Sidenote:
We have only two ways to tell Validator about something important to it. First one is the attribute name and the second one are rules. And that is important.


By now, i've tried couple of ways to handle array validation and here's a description of each one + more.

First approach. My original PR (#255) for Validator was pretty blunt:
During validation, if Validator finds a value and it's an array, then it's like a chap will go through each item of an array checking it against the rules.

In most cases that approach would have been enough, except only when we have to rules:

  • First one is exists. That is because we'll have DB query run for every item in the array.
  • Second one is any custom set rule. Validator will go through each item and apply custom rule to every one. Which leads to the same problem as above - what if user runs a query and wants to use "WHERE IN" in his custom rule. Won't be possible.

Second approach. Described in PR (#739) and uses special chars.
That approach uses special characters ([]) at the end of the attribute to tell Validator when to apply what type of validation. You could explicitly tell which rules must be applied to each item or to the array as a whole. Which is good because it fixed the problem of blunt approach. It is a bit hacky, but so far seems like only reasonable way around without huge Validator rewrite (have to change how rules are being handled, how custom rules are being handled, how classes extending Validator have to handle array validation etc.). So we're back to the drawing board.

Third approach. Special rule proposed by RSully in PR (#739).
With this approach you could set array rule which could be an indicator that that attribute is an array and must be validated item by item. Problem is, it's basically the same thing as a blunt approach because it's still impossible to tell Validator which rules exactly must be run on per item bases and which rules must be applied to the whole array - so we'll still have tons of DB queries on array with rule exists.


This is where i'm going to ask for help figuring out how to make Validator understand which rules do we want to run against array as a whole or against each item of the array individually.

Right now i have only one idea and it's crazy hacky. Add protected variable/flag to Validator containing an array of rules that must be validated as a whole array (exists). Then when validating, check if current rule is set in flag array. If it is, then do not validate each item individually (if value is an array). But, we still have problem with custom set rules - how should we validate them? Should they always be run item by item or array as a whole? It's pretty much impossible to tell Validator in it's current design how to handle these custom rules.


Looking forward to your ideas.

New PR: #753

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions