Skip to content

hexadecimal validation in v10 validator #974

@marcosartorato

Description

@marcosartorato

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions