-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
validate_min.go
65 lines (58 loc) · 1.36 KB
/
validate_min.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
package changeset
import (
"strconv"
"strings"
)
// ValidateMinErrorMessage is the default error message for ValidateMin.
var ValidateMinErrorMessage = "{field} must be more than {min}"
// ValidateMin validates the value of given field is not smaller than min.
// Validation can be performed against string, slice and numbers.
func ValidateMin(ch *Changeset, field string, min int, opts ...Option) {
val, exist := ch.changes[field]
if !exist {
return
}
options := Options{
message: ValidateMinErrorMessage,
}
options.apply(opts)
invalid := false
switch v := val.(type) {
case string:
invalid = len(v) < min
case []interface{}:
invalid = len(v) < min
case []*Changeset:
invalid = len(v) < min
case int:
invalid = v < min
case int8:
invalid = v < int8(min)
case int16:
invalid = v < int16(min)
case int32:
invalid = v < int32(min)
case int64:
invalid = v < int64(min)
case uint:
invalid = v < uint(min)
case uint8:
invalid = v < uint8(min)
case uint16:
invalid = v < uint16(min)
case uint32:
invalid = v < uint32(min)
case uint64:
invalid = v < uint64(min)
case uintptr:
invalid = v < uintptr(min)
case float32:
invalid = v < float32(min)
case float64:
invalid = v < float64(min)
}
if invalid {
r := strings.NewReplacer("{field}", field, "{min}", strconv.Itoa(min))
AddError(ch, field, r.Replace(options.message))
}
}