8000 Nested validate don't work with rules · Issue #221 · gookit/validate · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Nested validate don't work with rules #221

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

Closed
zKoz210 opened this issue Jul 28, 2023 · 8 comments
Closed

Nested validate don't work with rules #221

zKoz210 opened this issue Jul 28, 2023 · 8 comments
Assignees
Labels
bug Something isn't working enhancement New feature or request

Comments

@zKoz210
Copy link
zKoz210 commented Jul 28, 2023

System (please complete the following information):

  • OS: macOS
  • GO Version: 1.20
  • Pkg Version: 1.5.0

Describe the bug

nested data more than 1 level is incorrectly validated. while required works correctly

To Reproduce

	m := map[string]any{
		"clinics": []map[string]any{
			{
				"clinic_id": "1",
				"doctors": []map[string]any{
					{
						"doctor_id": 1,
						"dates": []map[string]string{
							{
								"date": "2023-01-01",
							},
						},
					},
				},
			},
		},
	}

	v := validate.Map(m)

	v.StringRule("clinics", "required|array")
	v.StringRule("clinics.*.clinic_id", "required|string")
	v.StringRule("clinics.*.doctors", "required|array")
	v.StringRule("clinics.*.doctors.*.doctor_id", "required")
	v.StringRule("clinics.*.doctors.*.dates", "required|array")
	v.StringRule("clinics.*.doctors.*.dates.*.date", "required|string")

	if v.Validate() { // validate ok
		safeData := v.SafeData()

		fmt.Println("Validation OK:")
		fmt.Println(safeData)
	} else {
		fmt.Println(v.Errors)       // all error messages
		fmt.Println(v.Errors.One()) // returns a random error message text
	}

Result:

clinics.*.doctors.*.dates.*.date:
 string: clinics.*.doctors.*.dates.*.date value must be a string
clinics.*.doctors.*.dates.*.date value must be a string

without additional rules (OK):

v.StringRule("clinics.*.doctors.*.dates.*.date", "required")

Result

Validation OK:
map[clinics:[map[clinic_id:1 doctors:[map[dates:[map[date:2023-01-01]] doctor_id:1]]]] clinics.*.clinic_id:[1] clinics.*.doctors:[[map[dates:[map[date:2023-01-01]] doctor_id:1]]] clinics.*.doctors.*.dates:[[[map[date:2023-01-01]]]] clinics.*.doctors.*.dates.*.date:[[[2023-01-01]]] clinics.*.doctors.*.doctor_id:[[1]]]

Expected behavior

Nested structures must validate correctly

@zKoz210
Copy link
Author
zKoz210 commented Jul 28, 2023

@sujit-baniya sorry to bother you, did your changes from the issue solve this problem?

@sujit-baniya
Copy link
Contributor

@zKoz210 Yes it did solve the issue for me. But it required a little bit of optimization so I used https://github.com/tidwall/gjson and final changes looked like
https://github.com/sujit-baniya/go-validate/blob/master/validating.go

@sujit-baniya
Copy link
Contributor

@zKoz210
Copy link
Author
zKoz210 commented Jul 28, 2023

@sujit-baniya can you make a pull request with these changes? it would be just wonderful

@sujit-baniya
Copy link
Contributor

@inhere Any suggestions/opinions before creating pull request from the forked repo?

zKoz210 added a commit to zKoz210/validate that referenced this issue Jul 31, 2023
@inhere
Copy link
Member
inhere commented Jul 31, 2023

hi @sujit-baniya @zKoz210
😄 You can get the latest code to test your case. It should be solved.

@inhere inhere added bug Something isn't working enhancement New feature or request labels Jul 31, 2023
@zKoz210
Copy link
Author
zKoz210 commented Jul 31, 2023

@inhere thanks for the quick response and solution to the problem, your package is the most convenient and functional. can you make a new release?

@zKoz210 zKoz210 closed this as completed Aug 1, 2023
@inhere
Copy link
Member
inhere commented Aug 1, 2023

Release a new version as soon as today.

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

No branches or pull requests

3 participants
0