go-playground / validator

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

Validation failed url with required_without #1296

Closed fooxlj07 closed 1 day ago

fooxlj07 commented 4 months ago

Package version v10:

Issue, Question or Enhancement:

Field Field1 stringvalidate:"required_without=Field2"`,url, if this field1 is empty, and field2 is not, the validation is failed Error:Key: 'Field1' Error:Field validation for 'Field1' failed on the 'url' tag`

Code sample, to showcase or reproduce:

package main

import (
    "fmt"

    "github.com/go-playground/validator/v10"
)

func main() {
    validate := validator.New()

    s := struct {
        Field1 string `validate:"required_without=Field2,url"`
        Field2 string `validate:"required_without=Field1"`
    }{
        // Field1: "ampq:cd.com",
        Field2: "field1",
    }

    fmt.Println(validate.Struct(s))
}
vladz commented 4 days ago

Field1 string validate:"required_without=Field2,url"

I've run into this problem too. I solved it by adding omitempty param, like this:

    Field1 string `validate:"required_without=Field2,omitempty,url"`