validator icon indicating copy to clipboard operation
validator copied to clipboard

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

Open santhosh77h opened this issue 1 year ago • 7 comments

  • [x] I have looked at the documentation here first?
  • [x] I have looked at the examples provided that may showcase my question here?

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.

santhosh77h avatar Mar 04 '24 12:03 santhosh77h

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 avatar Mar 07 '24 01:03 CyJaySong

@deankarn Hello, what do you think of this suggestion

CyJaySong avatar Mar 07 '24 01:03 CyJaySong

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.

deankarn avatar Mar 07 '24 06:03 deankarn

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

Orocker avatar Mar 21 '24 05:03 Orocker

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

CyJaySong avatar Mar 22 '24 00:03 CyJaySong

Any news?

gssr2023 avatar May 20 '24 15:05 gssr2023

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

gssr2023 avatar Jul 11 '24 08:07 gssr2023