somnambulist-tech / validation

A re-write of rakit/validation, a standalone validation library inspired by Laravel Validation
MIT License
44 stars 13 forks source link

Regex pipe validation error #2

Closed McHill007 closed 2 years ago

McHill007 commented 2 years ago

Hi,

a pipe as or in a regular expression is recognized as a new validation rule. I have replaced the preg_split in resolveRules. Maybe there is a nicer solution.

        if (is_string($rules)) {
            preg_match('/(?=[^|])(?:[^|]*\/[^)]+\/)*[^|]*/', $rules, $rules) ;
        }

Eric

dave-redfern commented 2 years ago

@McHill007 Hi, the current way to deal with this is to write the rule as an array instead of as a single string. That can be done in two ways:

[
    'field' => ['required', 'regex:/(this|that)/',],
]

or as:

[
    'field' => [
        'required',
        'regex' => '/(this|that)/',
    ],
]

I'll take a look at the regex; my only concern with adding it is if (when) more complex cases arise and it introduces further issues and becomes difficult to maintain.

dave-redfern commented 2 years ago

I've done some initial testing and it should be preg_match_all() but I also ran into a breaking regex case: regex:/[(this|that).,]/. Since a regex format can be largely anything, I honestly do not think this is possible to handle in a sane manner.

dave-redfern commented 2 years ago

I'm closing this now due to the above identified issue; however I am open to revisiting this if there is a sensible way of dealing with the content of a regex. Perhaps it needs to additionally include a prefix, but rules can be arbitrarily re-assigned making that difficult.

While the need to re-write as an array of rules is not as elegant, it is currently much easier to maintain.