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

Url does not error upon validation correctly? #1241

Open
52617365 opened this issue Mar 5, 2024 · 1 comment
Open

Url does not error upon validation correctly? #1241

52617365 opened this issue Mar 5, 2024 · 1 comment

Comments

@52617365
Copy link

52617365 commented Mar 5, 2024

Version: github.com/go-playground/validator/v10 v10.18.0

I'm aware that http_url tag is a thing but this seemed odd? Is this a bug?

func TestValidatorUrlBug(t *testing.T) {
	v := validator.New()

	type TestStruct struct {
		Website string `validate:"url"`
	}

	testData := TestStruct{
		Website: "htt://fake", // Invalid URL that should error on validation.
		// "htt:/fake" errors.
	}

	err := v.Struct(testData)
	if err == nil {
		t.Fatal("Expected validation errors, but got none")
	}

	validationErrors, ok := err.(validator.ValidationErrors)
	if !ok {
		t.Fatalf("Expected validator.ValidationErrors, got %T", err)
	}

	assert.NotNil(t, validationErrors)
	assert.Equal(t, "url", validationErrors[0].ActualTag())
}
@parrotmac
Copy link

This would appear to be expected (not a bug) as a URL can have any scheme. Popular ones include http, https, or ftp, but can be custom (e.g. docker:// used by Docker). See https://go.dev/play/p/fnBhc6O5dZ9 for an example.

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

No branches or pull requests

2 participants