validator
validator copied to clipboard
Url does not error upon validation correctly?
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())
}
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.