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

Required behaviour changed for time.Time #1162

Closed
2 tasks done
daveoxley opened this issue Sep 10, 2023 · 3 comments · Fixed by #1163
Closed
2 tasks done

Required behaviour changed for time.Time #1162

daveoxley opened this issue Sep 10, 2023 · 3 comments · Fixed by #1163

Comments

@daveoxley
Copy link

  • I have looked at the documentation here first?
  • I have looked at the examples provided that may showcase my question here?

Package version eg. v9, v10:

v10.15.2

Issue, Question or Enhancement:

A zero time.Time passes the required validation in v10.15.2 but not in v10.15.1. Adding in WithRequiredStructEnabled to the New() func does enable the test to pass but I'm not sure if I should make this code change in our codebase; should the default behaviour have changed between these minor releases?

I've provided a sample test that passes in v10.15.1 but fails in v10.15.2.

Code sample, to showcase or reproduce:

func TestTimeRequired(t *testing.T) {
	validate := New()
	validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
		name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]

		if name == "-" {
			return ""
		}

		return name
	})

	type TestTime struct {
		Time time.Time `validate:"required"`
	}

	var testTime TestTime

	err := validate.Struct(&testTime)
	NotEqual(t, err, nil)
	AssertError(t, err.(ValidationErrors), "TestTime.Time", "TestTime.Time", "Time", "Time", "required")
}
@MukeshGKastala
Copy link

+1

@deankarn
Copy link
Contributor

Thanks for the report, I’ll be looking into it as soon as I can find some time.

deankarn added a commit that referenced this issue Sep 13, 2023
## Fixes Or Enhances

Fixes #1162 by not skipping the `required` for non nested structs
accidentally introduced in last release.
@deankarn
Copy link
Contributor

@daveoxley this should all be resolved in Release https://github.com/go-playground/validator/releases/tag/v10.15.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants