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

Params undeclared when using regex #14

Closed MaxChri closed 1 year ago

MaxChri commented 1 year ago

If you use for example: "password": "required|between:8,16|regex:/^[\\da-zA-Z!$%+.]+$/"

Parameters like ":min" and ":max" are undeclared if the string is between 8-16 characters. The problem is that the "regex" validation could fail while "between" parameter is fine. If that happens, you will have a wrong response message with missing (e.g min/max) variables.

Custom response message: Your password must have between :min and :max characters and only [! $ % + .] as special characters.

How it should look like: Your password must have between 8 and 16 characters and only [! $ % + .] as special characters.

As you can see, instead of showing the parameter values, the key will be displayed in the response. If your password is not between 8-16 characters, the response is correct, showing the actual values instead of the keys. This issue was still there with rakit validatior.

dave-redfern commented 1 year ago

@MaxChri this happens because parameters are not shared between rule instances on the same attribute. I'll need to think how to handle this as parameter names are not unique across rules so a general collection would result in collisions e.g. different and required_if both define field

dave-redfern commented 1 year ago

@MaxChri 1.6.0 now allows referencing parameters from related rules on the same attribute. You can define a custom message using :<rule_name>.<parameter> so in your example if would be :between.min and :between.max on the regex message rule.

MaxChri commented 1 year ago

@dave-redfern it works perfectly. Thank you very much!