go-playground / validator

:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
MIT License
16.06k stars 1.29k forks source link

Url does not error upon validation correctly? #1241

Open 52617365 opened 4 months ago

52617365 commented 4 months ago

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 commented 3 months ago

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.