govalidator icon indicating copy to clipboard operation
govalidator copied to clipboard

Dynamic validation

Open bernielomax opened this issue 8 years ago • 2 comments

Hi,

Is it possible to set validation on a particular field dynamically. I.e. not by using tags? One problem I have is I want to validate my ID field but only when its being validated in an update method? ID should be missing when it is the create method.

Sorry if this is already answered somewhere.

Cheers,

bernielomax avatar Aug 05 '16 22:08 bernielomax

Hi! Currently its not possible with help of library, but you could to create your own middleware that will accept an object and validate all necessary fields inside itself, something like this:

import v "github.com/asaskevich/govalidator"
...
type User struct {
  Name string
  Email string
}
...
func ValidateMyStruct(u User) bool {
  return v.IsEmail(u.Email)
}

asaskevich avatar Aug 26 '16 07:08 asaskevich

Hello guys! I forked this package cause owner disappeared. Hope, he will be back, but it would be easier to merge these changes back if he is back Link to my repo: create issue there and we'll discuss it.

sergeyglazyrindev avatar Oct 17 '21 21:10 sergeyglazyrindev