Skip to content

Commit

Permalink
Add validator MapHasKey (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiendc authored Nov 27, 2024
1 parent b9c42a3 commit e931482
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
10 changes: 10 additions & 0 deletions base/map/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ func Len[K comparable, V any, M ~map[K]V](m M, min, max int) (bool, []base.Error
return false, []base.ErrorParam{{Key: kLen, Value: l}}
}

// HasKey checks map contains one or multiple keys
func HasKey[K comparable, V any, M ~map[K]V](m M, keys ...K) (bool, []base.ErrorParam) {
for _, k := range keys {
if _, exists := m[k]; !exists {
return false, []base.ErrorParam{{Key: kItemKey, Value: k}}
}
}
return true, nil
}

// KeyIn checks map keys must be in a list
func KeyIn[K comparable, V any, M ~map[K]V](m M, vals ...K) (bool, []base.ErrorParam) {
keys := gofn.MapKeys(m)
Expand Down
14 changes: 14 additions & 0 deletions base/map/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ func Test_Len(t *testing.T) {
assert.True(t, params[0].Key == kLen && params[0].Value == 2)
}

func Test_HasKey(t *testing.T) {
assert.True(t, gofn.Head(HasKey[int, int](map[int]int(nil))))
assert.True(t, gofn.Head(HasKey(map[int]int{})))
assert.True(t, gofn.Head(HasKey(map[int]int{1: 1, 2: 2}, 1, 2, 2, 1)))

ok, params := HasKey(map[int]int{1: 1, 2: 2}, 3)
assert.False(t, ok)
assert.True(t, params[0].Key == kItemKey && params[0].Value == 3)

ok, params = HasKey(map[int]int{1: 1, 2: 2}, 1, 2, 3)
assert.False(t, ok)
assert.True(t, params[0].Key == kItemKey && params[0].Value == 3)
}

func Test_KeyIn(t *testing.T) {
assert.True(t, gofn.Head(KeyIn[int, int](map[int]int(nil), 0, 1, 2)))
assert.True(t, gofn.Head(KeyIn[int, int](map[int]int(nil))))
Expand Down
1 change: 1 addition & 0 deletions internal/templates/en/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ var (

// Map
"map_len": `{{.Field}}: number of items must be in range {{.Min}} to {{.Max}}`,
"map_has_key": `{{.Field}}: map must contain keys {{.TargetValue}}`,
"map_key_in": `{{.Field}}: map keys must be one of {{.TargetValue}}`,
"map_key_not_in": `{{.Field}}: map keys must not be one of {{.TargetValue}}`,
"map_key_range": `{{.Field}}: map keys must be in range {{.Min}} to {{.Max}}`,
Expand Down
5 changes: 5 additions & 0 deletions validator_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ func MapLen[K comparable, V any, M ~map[K]V](m M, min, max int) SingleValidator
return call3[M]("len", mapType, "Min", "Max", mapFunc.Len[K, V, M])(m, min, max)
}

// MapHasKey validates the input map must have the specified keys
func MapHasKey[K comparable, V any, M ~map[K]V](m M, keys ...K) SingleValidator {
return call2N[M]("has_key", mapType, "TargetValue", mapFunc.HasKey[K, V, M])(m, keys...)
}

// MapKeyIn validates the input map must have keys in the specified values
func MapKeyIn[K comparable, V any, M ~map[K]V](m M, keys ...K) SingleValidator {
return call2N[M]("key_in", mapType, "TargetValue", mapFunc.KeyIn[K, V, M])(m, keys...)
Expand Down
8 changes: 8 additions & 0 deletions validator_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ func Test_MapLen(t *testing.T) {
assert.Equal(t, "len", errs[0].Type())
}

func Test_MapHasKey(t *testing.T) {
errs := MapHasKey(map[int]int{3: 3, 2: 2, 1: 1}, 1, 2, 3, 3, 2).Validate(ctxBg)
assert.Equal(t, 0, len(errs))

errs = MapHasKey(map[int]int{3: 3, 2: 2, 1: 1}, 1, 2, 3, 4).Validate(ctxBg)
assert.Equal(t, "has_key", errs[0].Type())
}

func Test_MapKeyIn(t *testing.T) {
errs := MapKeyIn(map[int]int{3: 3, 2: 2, 1: 1}, 1, 2, 3, 4, 5).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
Expand Down

0 comments on commit e931482

Please sign in to comment.