validator icon indicating copy to clipboard operation
validator copied to clipboard

hexadecimal validation in v10 validator

Open marcosartorato opened this issue 3 years ago • 1 comments

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

marcosartorato avatar Jul 07 '22 14:07 marcosartorato

+1

Antonboom avatar Aug 10 '22 07:08 Antonboom