forked from rezakhademix/govalidator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
min.go
62 lines (54 loc) · 1.85 KB
/
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
package govalidator
import (
"strings"
"unicode/utf8"
)
const (
// Min represents rule name which will be used to find the default error message.
Min = "min"
// MinString represents the rule name which will be used to find the default error message.
MinString = "minString"
// MinMsg is the default error message format for fields with Min validation rule.
MinMsg = "%s should be more than %v"
// MinStringMsg is the default error message format for fields with MinString validation rule.
MinStringMsg = "%s should have more than %v characters"
)
// MinInt checks if the given integer value is greater than or equal the given min value.
//
// Example:
//
// v := validator.New()
// v.MinInt(18, 0, "age", "age must be at least 0.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v Validator) MinInt(i, min int, field, msg string) Validator {
v.check(i >= min, field, v.msg(Min, msg, field, min))
return v
}
// MinFloat checks if the given float value is greater than or equal the given min value.
//
// Example:
//
// v := validator.New()
// v.MinFloat(5.0, 0.0, "height", "height must be at least 0.0 meters.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v Validator) MinFloat(f, min float64, field, msg string) Validator {
v.check(f >= min, field, v.msg(Min, msg, field, min))
return v
}
// MinString checks if the length of given string is greater than or equal the given min value.
//
// Example:
//
// v := validator.New()
// v.MinString("rey", 5, "name", "name should have more than 5 characters.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v Validator) MinString(s string, minLen int, field, msg string) Validator {
v.check(utf8.RuneCountInString(strings.TrimSpace(s)) >= minLen, field, v.msg(MinString, msg, field, minLen))
return v
}