-
Notifications
You must be signed in to change notification settings - Fork 0
/
validators.go
59 lines (53 loc) · 1.26 KB
/
validators.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
package validate
import (
"errors"
"fmt"
"regexp"
"strings"
)
func validateNotNull(path string, value interface{}) error {
if value != nil {
return nil
} else {
return errors.New("Value of " + path + " should not be nil")
}
}
func validateNoSpace(path string, value interface{}) error {
err, v := ConvertToString(value)
if err != nil {
return err
}
if strings.ContainsAny(v, " ") {
return errors.New("Value of " + path + " should not contains space")
}
return nil
}
func validateNotEmpty(path string, value interface{}) error {
err, v := ConvertToString(value)
if err != nil {
return err
}
if strings.Trim(strings.Trim(v, " "), " ") == "" {
return errors.New("Value of " + path + " should not be empty")
}
return nil
}
func validateEmail(path string, value interface{}) error {
err, v := ConvertToString(value)
if err != nil {
return err
}
Re := regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`)
if Re.MatchString(v) {
return nil
} else {
return errors.New("String of " + path + " must be an email, but none email string privided")
}
}
func ConvertToString(s interface{}) (error, string) {
if v, ok := s.(string); ok {
return nil, v
} else {
return errors.New(fmt.Sprintf("Value of %#v is not an email.", s)), ""
}
}