-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidator_map.go
67 lines (54 loc) · 2.93 KB
/
validator_map.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package validation
import (
"github.com/tiendc/go-validator/base"
mapFunc "github.com/tiendc/go-validator/base/map"
)
const (
mapType = "map"
)
// Map allows validating every map entry
func Map[K comparable, V any, M ~map[K]V](m M) MapContentValidator[K, V, M] {
return NewMapContentValidator(m)
}
// MapLen validates the input map must have length in the specified range
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...)
}
// MapNotHaveKey validates the input map must not have the specified keys
func MapNotHaveKey[K comparable, V any, M ~map[K]V](m M, keys ...K) SingleValidator {
return call2N[M]("not_have_key", mapType, "TargetValue", mapFunc.NotHaveKey[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...)
}
// MapKeyNotIn validates the input map must have keys not in the specified values
func MapKeyNotIn[K comparable, V any, M ~map[K]V](m M, keys ...K) SingleValidator {
return call2N[M]("key_not_in", mapType, "TargetValue", mapFunc.KeyNotIn[K, V, M])(m, keys...)
}
// MapKeyRange validates the input map must have keys in the specified range.
// Only applies to key type of number or string.
func MapKeyRange[K base.Number | base.String, V any, M ~map[K]V](m M, min, max K) SingleValidator {
return call3[M]("key_range", mapType, "Min", "Max", mapFunc.KeyRange[K, V, M])(m, min, max)
}
// MapValueIn validates the input map must have values in the specified values
func MapValueIn[K comparable, V comparable, M ~map[K]V](m M, values ...V) SingleValidator {
return call2N[M]("value_in", mapType, "TargetValue", mapFunc.ValueIn[K, V, M])(m, values...)
}
// MapValueNotIn validates the input map must have values not in the specified values
func MapValueNotIn[K comparable, V comparable, M ~map[K]V](m M, values ...V) SingleValidator {
return call2N[M]("value_not_in", mapType, "TargetValue", mapFunc.ValueNotIn[K, V, M])(m, values...)
}
// MapValueRange validates the input map must have values in the specified range.
// Only applies to value type of number or string.
func MapValueRange[K comparable, V base.Number | base.String, M ~map[K]V](m M, min, max V) SingleValidator {
return call3[M]("value_range", mapType, "Min", "Max", mapFunc.ValueRange[K, V, M])(m, min, max)
}
// MapValueUnique validates the input map must have unique values
func MapValueUnique[K comparable, V comparable, M ~map[K]V](m M) SingleValidator {
return call1[M]("value_unique", mapType, mapFunc.ValueUnique[K, V, M])(m)
}