I am using go Gin to create APIs in my project. I have requirement to create custom validators so I created like:
var valueone validator.Func = func(fl validator.FieldLevel) bool {
value, ok := fl.Field()
if ok {
if value != "one" {
return true
}
}
return true
}
var valuetwo validator.Func = func(fl validator.FieldLevel) bool {
value, ok := fl.Field()
if ok {
if value != "two" {
return true
}
}
return true
}
....
Instead of creating almost same custom validator multiple times, is there a way to create a single validator which is more generic and can be used for both cases, something like:
var value validator.Func = func(fl validator.FieldLevel, param) bool {
value, ok := fl.Field()
if ok {
if value != param {
return true
}
}
return true
}
I am not able to find a way to pass parameter to custom validator in gin or to make these generic validators through any other possible way. I have requirement to create like thousands of almost similar validator and I don't want to create custom validator for each one of them.