validate icon indicating copy to clipboard operation
validate copied to clipboard

Random error in validating required fields, returning error for only one field

Open tigoCaval opened this issue 1 year ago • 1 comments

System (please complete the following information):

  • OS: linux
  • GO Version: 1.23.2
  • Pkg Version: 1.5.4

Describe the bug

I'm trying to use the Gookit Validate library to validate input data with required field rules, but I'm encountering an issue: when running the test that validates the "name", "email", and "phone" fields as required, the validation returns errors randomly. In some test runs, the error is returned for the "name" field, in others for the "email" field, and in others for the "phone" field. Below is the test code I'm using:

To Reproduce

func TestValidateData(t *testing.T) {
	rules := map[string]string{
		"name":  "required",
		"email": "required",
		"phone": "required",
	}
	data := map[string]any{}

	v := New(data)
	v.StringRules(rules)

	if !v.Validate() {
		fmt.Println(v.Errors)
	}

	assert.Contains(t, v.Errors.All(), "name")
	assert.Contains(t, v.Errors.All(), "email")
	assert.Contains(t, v.Errors.All(), "phone")
	assert.Error(t, v.Errors)
}

Expected behavior

The expected behavior is that the validator should return errors for all the required fields that are missing ("name", "email", and "phone"). That is, the test should pass by checking that errors are returned for all the required fields.

Screenshots

Descrição da imagem

Additional context

Add any other context about the problem here.

tigoCaval avatar Jan 30 '25 02:01 tigoCaval

@tigoCaval default will stop on first validate error. You maybe shold config global options:

// change global options
validate.Config(func(opt *validate.GlobalOption) {
	opt.StopOnError = false
})

inhere avatar May 24 '25 10:05 inhere

Likely caused by Go's map iteration order being random. The validator seems to only report the first missing required field it encounters, and since map traversal order is random, the reported field changes on each run.

almas-x avatar Sep 08 '25 09:09 almas-x