govalidator icon indicating copy to clipboard operation
govalidator copied to clipboard

optional tag is not optional

Open maikelmclauflin opened this issue 6 years ago • 11 comments

not sure if i just set this up incorrectly but it seems like empty strings do not work too well.

I expect the following to pass because the property is optional (empty string allowed)

func TestValidator(t *testing.T) {
	type TestRequest struct {
		Hex string `valid:"hexadecimal,optional"`
	}
	run := func(body TestRequest) {
		isValid, err := govalidator.ValidateStruct(body)
		if err != nil {
			t.Error("an error was found", err)
		}
		if !isValid {
			t.Error(fmt.Sprintf("body is invalid: %#v", body))
		}
	}
	run(TestRequest{
		Hex: "0123456789abcdefABCDEF",
	})
	run(TestRequest{})
}

perhaps this is just my misunderstanding of how optional is used by govalidator

maikelmclauflin avatar Feb 24 '20 22:02 maikelmclauflin

seems like there is no check for optional in isFieldSet

maikelmclauflin avatar Feb 24 '20 23:02 maikelmclauflin

Hey, I'm facing the same problem. But it works with version 9 if this helps you...

belsakn avatar Feb 28 '20 13:02 belsakn

There are all tags changed to required, eg valid:"hexadecimal,optional", valid:"hexadecimal"

lrita avatar Apr 02 '20 10:04 lrita

Hi @asaskevich This is a breaking change bug by https://github.com/asaskevich/govalidator/commit/a0ca44a85ca1f5d68fb99b3211c877ea7163876f

lrita avatar Apr 02 '20 10:04 lrita

I am also having this issue, where optional fields are generating errors when they are not present

gbolo avatar Apr 30 '20 14:04 gbolo

Hi @asaskevich This is a breaking change bug by a0ca44a

This only works as expected now if you fetch the last commit before https://github.com/asaskevich/govalidator/commit/a0ca44a85ca1f5d68fb99b3211c877ea7163876f, like so

go get github.com/asaskevich/govalidator@772b7c5f8a56857abeff450a08976b680d67f732 

@asaskevich Any update on merging the open PR that patches this? https://github.com/asaskevich/govalidator/pull/385

ivanpk avatar Jun 11 '20 22:06 ivanpk

I have the same problem

pinyht avatar Jul 03 '20 04:07 pinyht

@asaskevich any ETA ?

nmccready avatar Aug 04 '20 17:08 nmccready

At the very least 772b7c5f8a56857abeff450a08976b680d67f732 needs to be tagged as a version as it's more solid than the latest.

nmccready avatar Aug 06 '20 21:08 nmccready

Hey @asaskevich , are PRs welcome for this issue?

profiralex avatar Aug 11 '20 15:08 profiralex

Hello guys! I forked this package cause owner disappeared. Hope, he will be back, but it would be easier to merge these changes back if he is back Link to my repo: create issue there and we'll discuss it.

sergeyglazyrindev avatar Oct 17 '21 21:10 sergeyglazyrindev