-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdefault.go
93 lines (82 loc) · 2.14 KB
/
default.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package govals
import (
"regexp"
)
// Email validate string if is valid email format,
// this function return bool and error
// if value does not valid
func Email(s string) (bool, error) {
return checkRule(s, emailRules)
}
// Numeric validate string contains digits only [0-9]
// this function return bool and error
// if value does not valid
func Numeric(s string) (bool, error) {
return checkRule(s, numericRule)
}
// Alpha validate string contains letters only [a-zA-Z]
// this function return bool and error
// if value does not valid
func Alpha(s string) (bool, error) {
return checkRule(s, alphaRule)
}
// AlphaNumeric validate string contains letters
// and number only [a-zA-Z0-9]
// this function return bool and error
// if value does not valid
func AlphaNumeric(s string) (bool, error) {
return checkRule(s, alphaNumericRule)
}
// Date checking string format as date
// like mm-dd-yyyy or mm/dd/yyyy
// this function return bool and error
// if value does not valid
func Date(s string) (bool, error) {
return checkRule(s, dateRule)
}
// Times checking string format time
// like 12:00:01 or 12:00
func Times(s string) (bool, error) {
return checkRule(s, timeRule)
}
// PhoneNumber checking string match phone number
// like +62828882888 or 082323333333
// this function return bool and error
// if value does not valid
func PhoneNumber(s string) (bool, error) {
return checkRule(s, phoneNumberRule)
}
func (d *defaultValsRule) validate() (bool, error) {
val := d.val.String()
switch d.tag {
case "email":
return checkRule(val, emailRules)
case "time":
return checkRule(val, timeRule)
case "date":
return checkRule(val, dateRule)
case "alpha":
return checkRule(val, alphaRule)
case "numeric":
return checkRule(val, numericRule)
case "alphaNumeric":
return checkRule(val, alphaNumericRule)
case "phone":
return checkRule(val, phoneNumberRule)
}
return true, nil
}
func checkRule(s, rule string) (bool, error) {
if len(s) == 0 {
return false, errEmptyValue
}
re, err := regexp.Compile(rule)
if err != nil {
return false, errInvalidRuleFormat
}
ok := re.MatchString(s)
if !ok {
return false, errInvalidFormat
}
return ok, nil
}