netmaker/validation/validation.go

24 lines
651 B
Go
Raw Normal View History

2021-10-27 00:27:29 +08:00
package validation
2021-07-25 04:13:24 +08:00
import (
"regexp"
2022-09-26 20:17:20 +08:00
validator "github.com/go-playground/validator/v10"
2021-07-25 04:13:24 +08:00
)
2021-10-09 03:07:12 +08:00
// CheckYesOrNo - checks if a field on a struct is yes or no
2021-07-25 04:13:24 +08:00
func CheckYesOrNo(fl validator.FieldLevel) bool {
return fl.Field().String() == "yes" || fl.Field().String() == "no"
}
2022-09-14 03:25:56 +08:00
// CheckYesOrNoOrUnset - checks if a field is yes, no or unset
func CheckYesOrNoOrUnset(fl validator.FieldLevel) bool {
return CheckYesOrNo(fl) || fl.Field().String() == "unset"
}
2021-10-09 03:07:12 +08:00
// CheckRegex - check if a struct's field passes regex test
2021-07-25 04:13:24 +08:00
func CheckRegex(fl validator.FieldLevel) bool {
re := regexp.MustCompile(fl.Param())
return re.MatchString(fl.Field().String())
}