This commit is contained in:
Jeffrey Duroyon
2019-04-04 14:29:48 +02:00
committed by Jeffrey Duroyon
parent 0df6d64c35
commit 33db360b03
38 changed files with 1476 additions and 1 deletions

View File

@@ -0,0 +1,34 @@
package validators
import (
"context"
"strings"
"gopkg.in/go-playground/validator.v9"
)
var (
CustomValidators = map[string]customValidator{
"enum": {
Message: "This field should be in: %v",
Validator: validateEnum,
},
"required": {
Message: "This field is required and cannot be empty",
},
}
)
type customValidator struct {
Message string
Validator validator.FuncCtx
}
func validateEnum(ctx context.Context, fl validator.FieldLevel) bool {
for _, v := range strings.Split(fl.Param(), " ") {
if v == fl.Field().String() {
return true
}
}
return false
}