go-playground / validator

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

Omitempty on a *string field does not work #1113

Closed Liudon closed 1 year ago

Liudon commented 1 year ago
// You can edit this code!
// Click here and start typing.
package main

import (
    "encoding/json"
    "fmt"

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

type Request struct {
    Samples []Info `json:"Infos" validate:"required,min=1"`
}

type Info struct {
    ID      *string
    Content *string `validate:"omitempty,min=1,max=10"`
}

func main() {

    str := `{"Infos":[{"Content":"1111111111111111111xxxxxxxx11","ID":"xx11"}]}`
    info := Request{}

    json.Unmarshal([]byte(str), &info)

    validate := validator.New()
    err := validate.Struct(info)

    fmt.Println(err)

}

output:

<nil>

max=10 not work

Liudon commented 1 year ago
type Request struct {
    Samples []Info `json:"Infos" validate:"required,min=1,dive"`
}

after add tag dive , it work ok