-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
51 lines (46 loc) · 1.14 KB
/
validate.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
package gocfg
import (
"fmt"
"regexp"
"strings"
)
func ValidateStringRegexpMatch(exp string) func(value interface{}) error {
return func(value interface{}) error {
v := value.(string)
ok, err := regexp.MatchString(exp, v)
if err != nil {
return err
}
if !ok {
return fmt.Errorf("value '%s' does not match regular expression '%s'", v, exp)
}
return nil
}
}
func ValidateStringContains(s string) func(value interface{}) error {
return func(value interface{}) error {
v := value.(string)
if ok := strings.Contains(v, s); !ok {
return fmt.Errorf("value '%s' does not contain '%s'", v, s)
}
return nil
}
}
func ValidateStringHasPrefix(s string) func(value interface{}) error {
return func(value interface{}) error {
v := value.(string)
if ok := strings.HasPrefix(v, s); !ok {
return fmt.Errorf("value '%s' does not start with '%s'", v, s)
}
return nil
}
}
func ValidateStringHasSuffix(s string) func(value interface{}) error {
return func(value interface{}) error {
v := value.(string)
if ok := strings.HasSuffix(v, s); !ok {
return fmt.Errorf("value '%s' does not end with '%s'", v, s)
}
return nil
}
}