gin icon indicating copy to clipboard operation
gin copied to clipboard

Access gin request Context from custom validators

Open samber opened this issue 3 years ago • 3 comments

The StructValidator interface does not allow passing *gin.Context.

For example, I would like to access c.Params from my validator.

Current interface signature:

type StructValidator interface {
	ValidateStruct(interface{}) error
	Engine() interface{}
}

It would be very nice to add an interface such as:

type StructValidatorWithContext interface {
	ValidateStructWithContext(interface{}) error
	Engine() interface{}
}

samber avatar May 30 '21 20:05 samber

Related to this: https://github.com/gin-gonic/gin/issues/1489

I agree that this could be helpful. The validator library supports contextual validation if you call (*Validate).StructCtx(...) instead of (*Validate).Struct(...). I imagine that could be implemented by passing *gin.Context as the ctx argument since it implements context.Context.

For example, in my case, I'd like to make a field required or not, based on a query param. If gin context was passed on to the validator, I could do that.

I imagine the StructValidator validator could be extended with one more method:

ValidateStructCtx(context.Context, interface{}) error

Or if this cannot be done, because it's a breaking change, then an "extension" interface could be defined, similar to what @samber proposed:

type StructValidatorWithContext interface {
	StructValidator
	ValidateStructCtx(context.Context, interface{}) error
}

And then, when binding, the code could check if the validator implementation is an instance of StructValidatorWithContext. Kind of like the standard library sometimes checks if your io.Reader is also an io.ReadCloser.

kszafran avatar Aug 05 '21 13:08 kszafran

@samber Check out my PR. I implemented passing context to validators: https://github.com/gin-gonic/gin/pull/2877

kszafran avatar Sep 22 '21 09:09 kszafran

I have also encountered this issue, which can help enhance the form validation function. I look forward to going online soon.

kaysonwu avatar Apr 21 '23 09:04 kaysonwu