-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlen.go
55 lines (48 loc) · 1.23 KB
/
len.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
package govals
import (
"fmt"
"reflect"
"strconv"
)
// RuneLength is validate text length
func RuneLength(s string, min, max int) (bool, error) {
return minMax(s, min, max)
}
// Length is validate number length
func Length(s int, min, max int) (bool, error) {
num := strconv.Itoa(s)
return minMax(num, min, max)
}
func (d *lengthValsRule) validate() (ok bool, err error) {
switch d.tagType.Type.Kind() {
case reflect.String:
val := d.val.String()
if d.count > 1 {
return minMax(val, d.min, d.max)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
val := d.val.Int()
if d.count > 1 {
return minMax(strconv.Itoa(int(val)), d.min, d.max)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
val := d.val.Uint()
return minMax(strconv.Itoa(int(val)), d.min, d.max)
}
return true, nil
}
func minMax(s string, min, max int) (bool, error) {
if len(s) == 0 {
return false, errEmptyValue
}
if min > max {
return false, fmt.Errorf("invalid rule, min must less than max")
}
if len(s) < min {
return false, fmt.Errorf("value must greater than %d", min)
}
if len(s) > max {
return false, fmt.Errorf("value must less than %d", max)
}
return true, nil
}