Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for validating against uuid values that are structs which implement the Stringer interface. #1189

Merged
merged 1 commit into from
Nov 4, 2023

Conversation

JoshGlazebrook
Copy link
Contributor

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 #900 (comment).

Make sure that you've checked the boxes below before you submit PR:

  • Tests exist or have been written that cover this particular change.

@go-playground/validator-maintainers

@JoshGlazebrook JoshGlazebrook requested a review from a team as a code owner October 30, 2023 19:44
@coveralls
Copy link

Coverage Status

coverage: 73.857% (+0.008%) from 73.849% when pulling fc09467 on JoshGlazebrook:uuid-stringer into 94a637a on go-playground:master.

@deankarn
Copy link
Contributor

deankarn commented Nov 4, 2023

@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())
		}
	}
}

@deankarn
Copy link
Contributor

deankarn commented Nov 4, 2023

I will merge and then make these changes.

@deankarn deankarn merged commit 4c1bd61 into go-playground:master Nov 4, 2023
12 checks passed
@JoshGlazebrook
Copy link
Contributor Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants