go-playground / validator

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

Order is not working w.r.t the required_with and len tag #1240

Open santhosh77h opened 7 months ago

santhosh77h commented 7 months ago

Package version eg. v9, v10:

v10

Issue, Question or Enhancement:

Question

Code sample, to showcase or reproduce:

package main

import "fmt"
import "github.com/go-playground/validator/v10"
type NomineeChangeInfo struct {
    Name  string `json:"name" validate:"required_with=Dob,max=40"`
    Dob   string `json:"dob" validate:"required_with=Name,len=10"`
}

func main() {
    fmt.Println("Hello, Go in CodeSandbox!")
    validate := validator.New()

    // Define a form with Email provided but Username missing
    formWithEmail := NomineeChangeInfo{
        Dob:"",
        Name:"",
    }

    // Validate the form
    err := validate.Struct(formWithEmail)
    if err != nil {
        fmt.Println("Validation error:", err)
    } else {
        fmt.Println("Validation successful")
    }
}

so in the above example for DOB we are getting the error as Error:Field validation for 'Dob' failed on the 'len' tag but there should have been no errro, if required_with is already in false state.

cyjaysong commented 6 months ago

required_if, required_unless,required_with,required_with_all,required_without,required_without_all,excluded_if,excluded_unless,excluded_with,excluded_with_all,excluded_without,excluded_without_all,skip_unless

if they meet the criteria for skipping, should skip all subsequent validation。

cyjaysong commented 6 months ago

@deankarn Hello, what do you think of this suggestion

deankarn commented 6 months ago

Yes that’s the way I expected those tags to work also, but they aren’t documented that way.

gonna have to double check if it ever behaved that way before. Will try to take a look this week, hopefully can find time.

Orocker commented 6 months ago
PromotionType          string                `json:"promotion_type" binding:"required,oneof= a b c"`
ApplicablePhase        []ApplicablePhaseItem `json:"applicable_phase" binding:"required_if=PromotionType a,dive,len=4"`         

@deankarn In this example, len tag not working when required_if meets condition

cyjaysong commented 6 months ago
PromotionType          string                `json:"promotion_type" binding:"required,oneof= a b c"`
ApplicablePhase        []ApplicablePhaseItem `json:"applicable_phase" binding:"required_if=PromotionType a,dive,len=4"`         

@deankarn In this example, len tag not working when required_if meets condition

it's not conform to the logic of this issues

gssr2023 commented 4 months ago

Any news?

gssr2023 commented 2 months ago

@deankarn any comments would be welcome. Is it even feasible for this to work the way some of us would like?