go-playground / validator

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

hexadecimal validation in v10 validator #974

Open marcosartorato opened 2 years ago

marcosartorato commented 2 years ago

Package version:

v10

Issue, Question or Enhancement:

An hexadecimal tag signals an invalid input over missing field even without the required tag

Code sample, to showcase or reproduce:

Over a ConfUp struct w/ optional hexadecimal Fopts field and a {} input, the validation returns a non-nil err.

type ConfUp struct{
    Fopts string `json:"fopts" validate:"hexadecimal"`
}
func (e *ConfUp) Validate() error {
    return validator.New().Struct(e)
}
func (e *ConfUp) FromByte(src []byte) error {
    return json.Unmarshal(src, e)
}
func (e *ConfUp) Byte() ([]byte, error) {
    return json.Marshal(e)
}
...
            cu := &ConfUp{}
            if ierr = cu.FromByte(payload); ierr == nil {
                if verr = cu.Validate(); verr == nil {
                    return nil
                }
            }
...

I expected no check over a missing field for no required tag. Moreover, a numeric tag does not trigger the same behavior. Are there some docu coverage over this logic?

The omitempty tag bypasses the issue

Antonboom commented 2 years ago

+1