Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Report: Erroneous Validation Error for Boolean Pointer Fields in Struct #239

Closed
kaptinlin opened this issue Oct 7, 2023 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@kaptinlin
Copy link
Contributor

System (please complete the following information):

  • OS: Windows [e.g. linux, macOS]
  • GO Version: 1.21.1 [e.g. 1.13]
  • Pkg Version: 1.5.1 [e.g. 1.1.1]

Describe the bug

When validating a struct with a boolean pointer field, an erroneous validation error is returned stating that the value must be a bool even when a boolean value is provided. This occurs specifically when the two_factor_enabled field in the Request struct is assigned a false boolean value.

To Reproduce

Here's the Go code to reproduce the issue:

package main

import (
	"fmt"
	"github.com/gookit/validate"
)

type Request struct {
	TwoFactorEnabled *bool `json:"two_factor_enabled" validate:"bool"`
}

func main() {
	twoFactorEnabled := false
	v := validate.New(&Request{
		TwoFactorEnabled: &twoFactorEnabled,
	})

	// Performing validation
	err := v.Validate()
	if err != nil {
		fmt.Println(v.Errors.One())
	}
}

When running this code, the following erroneous validation error is returned:

{
    "two_factor_enabled": {
        "bool": "two_factor_enabled value must be a bool"
    }
}

Expected behavior

The expected behavior is that the code should pass validation since a boolean value (false) is assigned to the two_factor_enabled field in the Request struct. No validation errors should be returned as the provided value satisfies the bool validation rule.

Screenshots

Not applicable.

Additional context

This unexpected validation error could potentially lead to issues when validating structs with boolean pointer fields. The validation should consider the assigned boolean value and not return an error when a valid boolean (either true or false) is provided. This seems to be an issue with the underlying validation logic for boolean fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants