guard icon indicating copy to clipboard operation
guard copied to clipboard

For some reason, I'm not getting the expected "shouldn't be blank" message

Open davecb opened this issue 4 years ago • 1 comments

Background: I'm looking at the first field in a line, and checking for blank...

2021/11/30 14:03:27 Read 13 fields, []string{"stf", "        1.0", "          v0.1.0", "      XX", "      XX", " XX", "       XX", "      1", "      1", "      unknown", "       1", "            0", "          0"}
2021/11/30 14:03:27 Read 13 fields, []string{"          ", " 1.0", "          v0.1.0", "      XX", "      XX", " XX", "       XX", "      1", "      1", "      unknown", "       1", "            0", "          0"}
2021/11/30 14:03:27 err = &validators.validationError{msg:"shouldn't be blank"}, err2 = &guard.errors{errs:[]error{(*validators.validationError)(0xc000068580)}}

The code is

	err1 := (&validators.StringNotBlank{Value: s}).Validate()
	err2 := guard.Validate(
		 (&validators.StringNotBlank{Value: s}),
	 )
	 if er1r != nil || err2 != nil {
		 log.Printf("err1 = %#v, err2 = %#v\n", err1, err2)
		 return err1.Error()
	 }
...

I was using the second, "err2", form of the code, and was getting just "validation errors", so I started reading the test functions and found the "err1" form, which returned "shouldn't be blank".

What I'd like to do is several validations at a time, such as one for blank, one for matching a list of strings, so I would like to use the "err2" form.

How should I do that and still get the "shouldn't be blank" form of the message?

davecb avatar Nov 30 '21 19:11 davecb

I may have found it in the test function:

    if errs, ok := err.(guard.Errors); ok {
	log.Printf("errs.ValidationErrors() = %q", errs.ValidationErrors())
	if len(errs.ValidationErrors()) != 2 {
		t.Fatalf("guard.Validate failed to return two validation errors")
	}
}

Could you add that to one of the early examples?

davecb avatar Dec 01 '21 14:12 davecb