8000 Return failed field if StopOnError · Issue #180 · gookit/validate · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Return failed field if StopOnError #180

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

Open
trapcodeio opened this issue Nov 14, 2022 · 3 comments
Open

Return failed field if StopOnError #180

trapcodeio opened this issue Nov 14, 2022 · 3 comments
Assignees
Labe 8000 ls
enhancement New feature or request question Further information is requested

Comments

@trapcodeio
Copy link

Is there a way to return the field that failed the validation when using StopOnError=true

for example

d := &SomeStruct{
 Name: "John"
 Email: "not an email"
}

v := validate.Struct(d)
if !v.validate() {
  // how do i get the exact field that failed this validation?
}
@inhere inhere self-assigned this Nov 14, 2022
@inhere inhere added the question Further information is requested label Nov 14, 2022
@inhere
Copy link
Member
inhere commented Nov 14, 2022

Please use v.Errors, see https://github.com/gookit/validate#validate-error

@trapcodeio
Copy link
Author

I saw that but it did not serve my purpose. This is what I resolved to do.

type validationError struct {
	Field string `json:"field"`
	Error string `json:"error"`
}

func parseError(errs validate.Errors) validationError {
	for k, _ := range errs {
		return validationError{k, errs.FieldOne(k)}
	}
	return validationError{}
}

This way I can return the failed field & message

{
    "field": "Email",
    "message": "Email value is invalid email address"
}

If there is a better way to achieve this, please let me know.

@inhere inhere added the enhancement New feature or request label Nov 14, 2022
@inhere
Copy link
Member
inhere commented Nov 14, 2022

Ok, thanks. I will add a helper method on Errors.

return like:

{
    "field": "Email",
    "message": "Email value is invalid email address"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants
0