-
Notifications
You must be signed in to change notification settings - Fork 4
/
between.go
38 lines (33 loc) · 1.22 KB
/
between.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
package govalidator
const (
// Between represents rule name which will be used to find the default error message.
Between = "between"
// BetweenMsg is the default error message format for fields with Between rule.
BetweenMsg = "%s should be greater than or equal %v and less than or equal %v"
)
// BetweenInt checks the value under validation to have an integer value between the given min and max.
//
// Example:
//
// v := validator.New()
// v.BetweenInt(21, 1, 10, "age", "age must be between 1 and 10.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v Validator) BetweenInt(i, min, max int, field, msg string) Validator {
v.check(i >= min && i <= max, field, v.msg(Between, msg, field, min, max))
return v
}
// BetweenFloat checks the field under validation to have a float value between the given min and max.
//
// Example:
//
// v := validator.New()
// v.BetweenFloat(3.5, 2.0, 5.0, "height", "height must be between 2.0 and 5.0 meters.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v Validator) BetweenFloat(f, min, max float64, field, msg string) Validator {
v.check(f >= min && f <= max, field, v.msg(Between, msg, field, min, max))
return v
}