-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.go
More file actions
47 lines (39 loc) · 997 Bytes
/
validators.go
File metadata and controls
47 lines (39 loc) · 997 Bytes
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
package validation
import (
"regexp"
"github.com/mirango/framework"
)
var (
RegexpNumber = regexp.MustCompile("^[0-9]+$")
RegexpFloat = regexp.MustCompile("^[+-]{0,1}([0-9]*[.])?[0-9]+$")
RegexpAlpha = regexp.MustCompile("^[a-zA-Z]+$")
)
func Eq(a interface{}) Validator {
return NewFuncValidator(func(c framework.Context, v framework.ParamValue) error {
if a != v.Value() {
return ErrEq.Err([]interface{}{v.Name(), a}...)
}
return nil
}, MsgEq.Msg(a))
}
func Number() Validator {
return Regexp(RegexpNumber)
}
func Alpha() Validator {
return Regexp(RegexpAlpha)
}
func Regexp(p *regexp.Regexp) Validator {
return NewFuncValidator(func(c framework.Context, v framework.ParamValue) error {
if !p.MatchString(v.RawString()) {
return ErrRegexp.Err([]interface{}{v.Name(), p.String()}...)
}
return nil
}, MsgRegexp.Msg(p.String()))
}
func StringRegexp(pstr string) Validator {
p, err := regexp.Compile(pstr)
if err != nil {
return nil
}
return Regexp(p)
}