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

Make url and http_url work with url.URL. #1231

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bfabio
Copy link

@bfabio bfabio commented Feb 14, 2024

Fixes Or Enhances

This makes the url and http_url validations work with url.URL or any newtype that implements the Stringer interface. I'm not familiar with the codebase so it's kind of a PoC PR to see if you think this can be useful.

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

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

But they don't pass, if this is a feature we want and it's the right approach I can investigate deeper. I suspect the reflection magic isn't working as I expected.

@go-playground/validator-maintainers

@bfabio bfabio requested a review from a team as a code owner February 14, 2024 13:03
Comment on lines +1462 to 1466
if stringer, ok := field.Interface().(fmt.Stringer); ok {
s = stringer.String()
} else {
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if stringer, ok := field.Interface().(fmt.Stringer); ok {
s = stringer.String()
} else {
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
}
if stringer, ok := field.Interface().(fmt.Stringer); !ok {
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
}
s = stringer.String()

return false
}
url, err := url.Parse(s)
if err != nil || url.Scheme == "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if err != nil || url.Scheme == "" {
if err != nil {


return true
if url.Host == "" && url.Fragment == "" && url.Opaque == "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if url.Host == "" && url.Fragment == "" && url.Opaque == "" {
if url.Scheme == "" || (url.Host == "" && url.Fragment == "" && url.Opaque == "") {

@nodivbyzero
Copy link
Contributor

I've added a few comments suggesting readability improvements.
I recommend keeping the original test functions unchanged and adding new ones for the Stringer type. Also, ensure the GitHub Actions checks pass.

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.

2 participants