-
Notifications
You must be signed in to change notification settings - Fork 30
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
Useful code in Validation errors? #4
Comments
I like option 3, and I'd welcome a PR like that. It's a good enhancement. And then just fyi, I'm not sure if you've you seen this hook? This ultimately ends up going here: Which gives you a place to translate swagger errors into something more acceptable for your app. |
Great, I'll work on the PR soon. The hook is interesting, but I only generate the models from swagger. We have an existing code base with lots of custom code and I am adding swagger elements to a new feature we develop. The models are easy to import, the rest requires a rework of the whole app.... |
|
Hi, I came across this package, as I am auto-generating my input and output models using
go-swagger
. This is very nice, as I am able to generate my parsing and validation logic from the same source as my documentation, making the design -> documentation <-> development workflow very streamlined. So, first off, thanks for all the hard work you all have done to make such a useful toolkit.However, I came across a usability issue with the errors, I would like to address, but would like feedback before making a fork. In short, the error code for all validation errors is exactly the same (422), which is problematic, when I want to generate custom validation errors in our api. Our api returns validation erros for each field with an error code, which is translated in the frontend (javascript SPA, iOS app). We use one error code for required, one for duplicate, one for must be true, one for invalid length, one for invalid data, etc... So, I was writing some code to translate the
go-openapi/errors/Validation
type into our custom type. But there is no easy way to introspect an error to find its cause.Basically, my input validation code (auto-generated by
go-swagger
) looks something like this:validate.Required (from
go-openapi/validate
) is this:and errors.Required (from
go-openapi/errors
) is this:Name
gives me the field name that has an error (very nice),In
is alwaysbody
for parsing POST/PUT body,message
(returned byError()
) is some text that does depend on the error type, andcode
(returned byCode()
) is always 422 for every error.I now have three approaches.
invalid
, giving the error message as extra context, which makes our response not so useful and not translatable.code
variable to each different type ofValidation
error that could be used by a switch statement to determine the type (ideally as a set of exportyed named constants form the errors package.The third choice seems the most stable, extensible, and useful for me and others, but I don't know if this would break some other package. Or if there was a conscious decision to use this http error code here?
If you like the approach, I would be happy to prepare a pull request with approach number 3
Cheers,
Ethan
The text was updated successfully, but these errors were encountered: