I try to validate if enum is valid in Golang using Gin framework.
I came across this solution:
But disadvantage of this approach is hardcoded value which we have to change manually every time enum value has changed.
Is there any way to get enum by its name as string without creating and register the map with types?
Desired result:
package main
import "github.com/go-playground/validator"
type Status int
const (
Single Status = iota
Married
Other
)
type User struct {
Status Status `json:"status" binding:"Enum=Status"`
}
func Enum(fl validator.FieldLevel) bool {
enumType := fl.Param() // Status
// get `Status` by `enumType` and validate it...
return true
}
func main() {}