You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I created a custom regexp validator, and it works for one regular expression but panics on another. I've confirmed the regular expression that paniced is a valid regular expression and works properly with my custom validator function, so I assume this is a bug with the internals of this package.
I am getting this error:
panic: Undefined validation function'private' on field 'Visibility'
regex
a regex validator won't be added because commas and = signs can be part
of a regex which conflict with the validation definitions. Although
workarounds can be made, they take away from using pure regex's.
Furthermore it's quick and dirty but the regex's become harder to
maintain and are not reusable, so it's as much a programming philosiphy
as anything.
In place of this new validator functions should be created; a regex can
be used within the validator function and even be precompiled for better
efficiency within regexes.go.
And the best reason, you can submit a pull request and we can keep on
adding to the validation library of this package!
To summarize I highly recommend you don't use a regex validator, many of the common characters used in regexes will conflict with the built-in characters when the tags are parsed; in your specific case the | is a special character that represents and or.
I have added the ability to work around such things, also in the docs:
Pipe ("|") is the 'or' validation tags separator. If you wish to have a pipe included within the parameter i.e. excludesall=| you will need to use the UTF-8 hex representation 0x7C, which is replaced in the code as a pipe, so the above will become excludesall=0x7C
typeTeststruct {
Field`validate:"excludesall=|"`// BAD! Do not include a a pipe!Field`validate:"excludesall=0x7C"`// GOOD! Use the UTF-8 hex representation.
}
Ah, thank you for the explanation. In fact, I think the approach of making custom validators per regular expression is the better way to go, because it makes things more maintainable.
I ran into the issue of having to copy/paste a regexp over to multiple validators, didn't think of the approach of adding a custom validator for a specific regular expression and referring to it by name.
Package version eg. v8, v9:
v9
Issue, Question or Enhancement:
I created a custom
regexp
validator, and it works for one regular expression but panics on another. I've confirmed the regular expression that paniced is a valid regular expression and works properly with my custom validator function, so I assume this is a bug with the internals of this package.I am getting this error:
This is the validation tag that panics:
validate:"required,regexp=^(public|private|unlisted)$"
This is the working validation tag:
validate:"required,min=1,regexp=^[a-zA-Z0-9_]*$"
Code sample, to showcase or reproduce:
This is my custom validator:
The text was updated successfully, but these errors were encountered: