-
Notifications
You must be signed in to change notification settings - Fork 8k
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
Map validation, contextual validation, validator tags for custom map/slice types #2877
base: master
Are you sure you want to change the base?
Conversation
14b4deb
to
7971f15
Compare
Codecov Report
@@ Coverage Diff @@
## master #2877 +/- ##
==========================================
+ Coverage 98.73% 98.76% +0.02%
==========================================
Files 41 41
Lines 3080 3150 +70
==========================================
+ Hits 3041 3111 +70
Misses 27 27
Partials 12 12
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
c790e2a
to
c52063e
Compare
@appleboy @thinkerou Would you have the time to review this PR? I suggest looking at it commit by commit, since each commit is self-contained. |
ca15e39
to
1a60ea4
Compare
I did some renaming ( |
Turns out that this may break custom validator tags if they use |
This way callers can always expect ValidationErrors, and in case of slice validation, they can also get indexes of the failing elements.
By passing the Gin context to bindings, custom validators can take advantage of the information in the context.
I have reworked my implementation. Now it does not rely on |
how? |
Hey, what exactly are you asking about? |
I have found an edge case when validating non-nil pointers with nil value. It can be fixed by changing case reflect.Ptr:
return v.ValidateStructContext(ctx, value.Elem().Interface()) to case reflect.Ptr:
if value.IsNil() {
return nil
}
return v.ValidateStructContext(ctx, value.Elem().Interface()) in I'm not updating the commits, though, because the maintainers don't seem to be particularly interested in this PR anyway. |
} | ||
|
||
for _, key := range value.MapKeys() { | ||
if err := v.ValidateStructContext(ctx, value.MapIndex(key).Interface()); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it will be more clear to reverse the condition and add the continue
statement since the indentation level will be decreased.
Great feature, that will allow me to refactor a bunch of validation code. Hope this gets merged soon! 🙏 |
This PR has been here for a long time. I kept it open just in case, but I'm not even updating it anymore. The chances of getting it merged seem slim. I could try to rebase it, though, if the maintainers are interested in merging it. |
Any chance we can get this rolling? |
This pull request offers a few simple, but very useful improvements:
(*Validator).RegisterValidationCtx
or(*Validator).RegisterStructValidationCtx
) will be passed the Gin context (ascontext.Context
). Resolves Access gin request Context from custom validators #2741validator.ValidationErrors
, without any custom types. This way callers can handle all validation errors in the same manner. Related to: Export struct sliceValidateError to allow error casting #2777A further improvement would be to merge this PR: #2825. That will enable getting the actual
*gin.Context
in custom validators, resolving #1489.