asaskevich / govalidator

[Go] Package of validators and sanitizers for strings, numerics, slices and structs
MIT License
6.04k stars 555 forks source link

the struct will be validated too many times #335

Open jacking212 opened 5 years ago

jacking212 commented 5 years ago

the struct type with a valid tag was validated here , https://github.com/asaskevich/govalidator/blob/f9ffefc3facfbe0caee3fea233cbb6e8208f4541/validator.go#L767

but it would be validated here another time. https://github.com/asaskevich/govalidator/blob/f9ffefc3facfbe0caee3fea233cbb6e8208f4541/validator.go#L1205

for example:

package main

import (
    "fmt"
    govalidator "github.com/asaskevich/govalidator"
)
type Deep1 struct {
    Deep1Val Deep2 `json:"deep_1_val" valid:"required"`
}

type Deep2 struct {
    Deep2Vals []Deep3 `json:"deep_2_vals" valid:"required"`
}

type Deep3 struct {
    Name string `json:"name" valid:"required~must set deep3.name"`
}

func main() {

    dp2 := Deep2{
        Deep2Vals: []Deep3{
            {Name: ""},
        },
    }
    dp1 := Deep1{
        Deep1Val: dp2,
    }
    fmt.Println(govalidator.ValidateStruct(&dp2))
    fmt.Println(govalidator.ValidateStruct(&dp1))
}
// if we define a struct with a Deep1 field and set valid tag, it will print error info three times when validate it
// output:
//false must set deep3.name
//false must set deep3.name;must set deep3.name

how about change this line to "return true,nil"? https://github.com/asaskevich/govalidator/blob/f9ffefc3facfbe0caee3fea233cbb6e8208f4541/validator.go#L1205

sergeyglazyrindev commented 3 years ago

Hello guys! I forked this package cause owner disappeared. Hope, he will be back, but it would be easier to merge these changes back if he is back Link to my repo: create issue there and we'll discuss it.