Skip to content

Commit

Permalink
add Enum string to validators (#5697)
Browse files Browse the repository at this point in the history
* feature: add Enum string to validators

* feature: add information for Enum errors
  • Loading branch information
zhyclouds authored Aug 22, 2024
1 parent 8ee564a commit d824759
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/validation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ Struct Tag Functions:
Tel
Phone
ZipCode
Enum

## LICENSE

Expand Down
5 changes: 5 additions & 0 deletions core/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ func (v *Validation) ZipCode(obj interface{}, key string) *Result {
return v.apply(ZipCode{Match{Regexp: zipCodePattern}, key}, obj)
}

// Enum Test that the obj is in the specified enumeration if type is string
func (v *Validation) Enum(obj interface{}, vals string, key string) *Result {
return v.apply(Enum{vals, key}, obj)
}

func (v *Validation) apply(chk Validator, obj interface{}) *Result {
if nil == obj {
if chk.IsSatisfied(obj) {
Expand Down
16 changes: 16 additions & 0 deletions core/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,22 @@ func TestZipCode(t *testing.T) {
}
}

func TestEnum(t *testing.T) {
valid := Validation{}

if valid.Enum("sms_code", "sms|email|code", "enum").Ok {
t.Error("\"sms_code\" is in the enum list of \"sms|email|code\" should be false")
}

if !valid.Enum("sms", "email|sms|code", "enum").Ok {
t.Error("\"sms\" is in the enum list of \"email|sms|code\" should be true")
}

if valid.Enum(200, "code|email|sms", "enum").Ok {
t.Error("200 is in the enum list of \"code|email|sms\" should be false")
}
}

func TestValid(t *testing.T) {
type user struct {
ID int
Expand Down
35 changes: 35 additions & 0 deletions core/validation/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var MessageTmpls = map[string]string{
"Tel": "Must be valid telephone number",
"Phone": "Must be valid telephone or mobile phone number",
"ZipCode": "Must be valid zipcode",
"Enum": "Must be a string value in \"%s\"",
}

var once sync.Once
Expand Down Expand Up @@ -738,3 +739,37 @@ func (z ZipCode) GetKey() string {
func (z ZipCode) GetLimitValue() interface{} {
return nil
}

// Enum Requires that the field must be within the enumerated value
type Enum struct {
Rules string
Key string
}

// IsSatisfied judge whether obj is valid
func (e Enum) IsSatisfied(i interface{}) bool {
if val, ok := i.(string); ok {
roles := strings.Split(e.Rules, "|")
for _, v := range roles {
if val == strings.TrimSpace(v) {
return true
}
}
}
return false
}

// DefaultMessage return the default Enum error message
func (e Enum) DefaultMessage() string {
return fmt.Sprintf(MessageTmpls["Enum"], e.Rules)
}

// GetKey return the e.Key
func (e Enum) GetKey() string {
return e.Key
}

// GetLimitValue return nil now
func (Enum) GetLimitValue() interface{} {
return nil
}

0 comments on commit d824759

Please sign in to comment.