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

Enhancement - Return Full Request JSON With Validation Errors #338

Closed
trvsysadmin opened this issue Jan 19, 2018 · 3 comments
Closed

Enhancement - Return Full Request JSON With Validation Errors #338

trvsysadmin opened this issue Jan 19, 2018 · 3 comments
Assignees
Labels

Comments

@trvsysadmin
Copy link

trvsysadmin commented Jan 19, 2018

Package version eg. v8, v9:

v9

Issue, Question or Enhancement:

I have two clarification-question / suggestions:

  1. We like to send back an error message to front-end-client the full details of the validation errors that occurred in a nested struct. After changing the namespace from the struct namespace to a json tag, invoking FieldError.Namespace() gives us orchestra.group_one.violin_small. We end up using another library called gabs to generate a combined nested json. I have put the sample code below and the sample output in json. It is pure luck I found gabs after a bit of googling as I was planning to write a json generator and inserter and that would have taken some time.
    Question is a) am I doing the right thing (does this library provide this functionality out of the box) and b) maybe it could be helpful to provide this kind of code as an example of how you might return jsoni-fied responses of validation errors.

  2. EDIT: Found that FieldError.Param() is what I was looking for. But weird that Param would return only the last of an OR operator (e.g. a|b|c -> c).
    ORIGINAL: Both FieldError.Tag() and FieldError.ActualTag() return only eq but not eq=something. But it does return a full eq=a|eq=b if I put the | operator. Is there anyway to return what its supposed to be equal to?

Code sample, to showcase or reproduce:

import (
	"encoding/json"

	"github.com/Jeffail/gabs"
	"gopkg.in/go-playground/validator.v9"
)

if err = validator.Struct(RequestStruct); err != nil {
		validationErrors := err.(validator.ValidationErrors)
		validationErrFriendlyJson := gabs.New()
		for _, v := range validationErrors {
			inputVsExpected := []string{v.Value().(string), v.ActualTag()}
			validationErrFriendlyJson.SetP(inputVsExpected, v.Namespace())
		}		
		context.Respond(validationErrFriendlyJson.Data())
		return
	}
{
    "error_message": "Validation Error",
    "error_object": {
        "Blah1": {
            "Blah2": {
                "Blah3": {
                    "Blah4": [
                        "WhatYouGave",
                        "eq=a|eq=b"
                    ]
                },
                "blah5": [
                    "WhatYouGave",
                    "eq"
                ]
            }
        }
    }
}
@trvsysadmin trvsysadmin changed the title Enhancement - Return Full Request JSON But With Validation Errors Enhancement - Return Full Request JSON With Validation Errors Jan 19, 2018
@deankarn deankarn self-assigned this Jan 19, 2018
@deankarn
Copy link
Contributor

Hey @trvsysadmin

for 2 I understand the confusion, the only thing I can think of is in the future have param return []string; but even that's weird as the param values may not line up with the tags as not all have to have a param.

As for 1 that's pretty subjective question/suggestion; normally I range through the ValidationErrors and build an array of objects containing the namespace and an error message, and in the client code the namespace matches the struct namespace(perhaps with json tags) and then sending that array back to the client there can be generic code to find objects and set errors on each field; but that's just how I'd use it.

I can see how it would be useful to send just the ValidationErrors as JSON back to the client side if it's going to handle the error messages; I'll look at making it JSON marshallable.

so to try and answer your questions:
a) It's up to you decide if you're doing the right thing, validator only provides the error information for your use and does not stick it's nose into what you do afterwards.
b) I'd accept a PR adding this as an example; be sure to note which framework you're using to put it into context for those who read it :)

Is there anything else or any other questions I can attempt to answer?

@trvsysadmin
Copy link
Author

trvsysadmin commented Jan 20, 2018

Nope that is all, thank you very much for your reply and thanks for your library!

Team here agrees 1 is subjective and may not be the job of the core library. Will try to submit a PR for as an example.

In general for 2, we also found out that for cases like len, we can't quite find out what the evaluation result was. For example:

"user_friendly_message": "{{fieldName}} - length (not value!) must be greater than 3. Your input: [[120]]."

is what we can get now but we would like to get:

"user_friendly_message": "{{fieldName}} - length (not value!) must be greater than 3. Your len: 1."

@deankarn
Copy link
Contributor

Thanks for your feedback about the evaluation result, I’ll definitely try and think of a way to incorporate that in the results if it makes sense :)

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

No branches or pull requests

2 participants