validator icon indicating copy to clipboard operation
validator copied to clipboard

Url does not error upon validation correctly?

Open 52617365 opened this issue 1 year ago • 1 comments

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

52617365 avatar Mar 05 '24 08:03 52617365

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.

parrotmac avatar Mar 26 '24 21:03 parrotmac