Closed JoshGlazebrook closed 11 months ago
@JoshGlazebrook this PR is close :)
But I think it will fail for alias types that an implement fmt.Stringer
. The type needs to be checked to see if the kind is already a string before attempting the cast. eg.
type uuidAlias string
func (u uuidAlias) String() string {
return "This is a UUID " + string(u)
}
I think the new helper can be changed like so to avoid this and maintain 100% backward compatibility:
func fieldMatchesRegexByStringerValOrString(regex *regexp.Regexp, fl FieldLevel) bool {
switch fl.Field().Kind() {
case reflect.String:
return regex.MatchString(fl.Field().String())
default:
if stringer, ok := fl.Field().Interface().(fmt.Stringer); ok {
return regex.MatchString(stringer.String())
} else {
return regex.MatchString(fl.Field().String())
}
}
}
I will merge and then make these changes.
Thank you!
Fixes Or Enhances
This adds the ability to validate UUIDs that have an underlying struct value but also implement the Stringer interface. This should cover most UUID implementations (google's uuid, etc).
Implements: #900, specifically https://github.com/go-playground/validator/issues/900#issuecomment-1046030775.
Make sure that you've checked the boxes below before you submit PR:
@go-playground/validator-maintainers