Skip to content

[Bug]: required tag treats int fields with value 0 as missing, causing false validation failures #1455

@jcoelho93

Description

@jcoelho93

What happened?

Hi,

I've noticed that the required validation tag treats an int field with value 0 as missing, which causes unexpected validation failures.

For example, this struct:

type Calendar struct {
    DayOfWeek int `json:"dayOfWeek" validate:"required,min=0,max=6"`
}

fails validation when DayOfWeek is set to 0, even though 0 is a valid and intentional value in this context.

Sample test code:

package main

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

type Calendar struct {
    DayOfWeek int `json:"dayOfWeek" validate:"required,min=0,max=6"`
}

func main() {
    inputValidator := validator.New()

    err := inputValidator.Struct(Calendar{DayOfWeek: 0})
    if err != nil {
        for _, err := range err.(validator.ValidationErrors) {
            println("Validation error:", err.Namespace(), "failed on the", err.Tag(), "tag")
        }
    } else {
        println("Validation passed")
    }
}

This results in a validation error:

Validation error: Calendar.DayOfWeek failed on the required tag

Version

v10.27.0

Example Code

package main

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

type Calendar struct {
	DayOfWeek int `json:"dayOfWeek" validate:"required,min=0,max=6"`
}

func main() {
	inputValidator := validator.New()

	err := inputValidator.Struct(Calendar{DayOfWeek: 0})
	if err != nil {
		for _, err := range err.(validator.ValidationErrors) {
			println("Validation error:", err.Namespace(), "failed on the", err.Tag(), "tag")
		}
	} else {
		println("Validation passed")
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions