forked from rs/rest-layer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
61 lines (57 loc) · 1.23 KB
/
string.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
package schema
import (
"errors"
"fmt"
"regexp"
"strings"
)
// String validates string based values
type String struct {
re *regexp.Regexp
Regexp string
Allowed []string
MaxLen int
MinLen int
}
// Compile compiles and validate regexp if any
func (v *String) Compile() (err error) {
if v.Regexp != "" {
// Compile and cache regexp, report any compilation error
if v.re, err = regexp.Compile(v.Regexp); err != nil {
err = fmt.Errorf("invalid regexp: %s", err)
}
}
return
}
// Validate validates and normalize string based value
func (v String) Validate(value interface{}) (interface{}, error) {
s, ok := value.(string)
if !ok {
return nil, errors.New("not a string")
}
l := len(s)
if l < v.MinLen {
return nil, fmt.Errorf("is shorter than %d", v.MinLen)
}
if v.MaxLen > 0 && l > v.MaxLen {
return nil, fmt.Errorf("is longer than %d", v.MaxLen)
}
if len(v.Allowed) > 0 {
found := false
for _, allowed := range v.Allowed {
if s == allowed {
found = true
break
}
}
if !found {
return nil, fmt.Errorf("not one of [%s]", strings.Join(v.Allowed, ", "))
}
}
if v.Regexp != "" {
if !v.re.MatchString(s) {
return nil, fmt.Errorf("does not match %s", v.Regexp)
}
}
return s, nil
}