Skip to content

Commit

Permalink
checker: fix list validator
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhan005 committed Aug 24, 2024
1 parent 73512fa commit 8b55ab6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ func equal(c CheckerContext) *ErrContext {
func list(c CheckerContext) *ErrContext {
ctx := NewErrorContext(c)
value := fmt.Sprintf("%v", ctx.FieldValue)
allowValues := strings.Split(c.Rule.params[0], ",")
allowValues := c.Rule.params
for _, v := range allowValues {
if value == v {
return nil
Expand Down
9 changes: 9 additions & 0 deletions checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,15 @@ func Test_List(t *testing.T) {
errs, ok = Check(v)
assert.True(t, ok)
assert.Nil(t, errs)

v = struct {
Role string `valid:"list:admin,editor,viewer" label:"角色"`
}{
Role: "editor",
}
errs, ok = Check(v)
assert.True(t, ok)
assert.Nil(t, errs)
}

func Test_StructSlice(t *testing.T) {
Expand Down

0 comments on commit 8b55ab6

Please sign in to comment.