Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
santhosh77h opened this issue Mar 4, 2024 · 5 comments
Open
2 tasks done

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

santhosh77h opened this issue Mar 4, 2024 · 5 comments

Comments

@santhosh77h
Copy link

santhosh77h commented Mar 4, 2024

  • I have looked at the documentation here first?
  • 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.

@CyJaySong
Copy link

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
Copy link

@deankarn Hello, what do you think of this suggestion

@deankarn
Copy link
Contributor

deankarn commented Mar 7, 2024

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
Copy link

Orocker commented Mar 21, 2024

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants