-
Notifications
You must be signed in to change notification settings - Fork 14
/
validators.go
54 lines (47 loc) · 1.19 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
package clif
import (
"fmt"
"regexp"
)
// IsAny joins a set of validator methods and returns true if ANY of them matches
func IsAny(v ...ParseMethod) ParseMethod {
return func(name, value string) (string, error) {
var err error
replace := value
for _, c := range v {
if replace, err = c(name, replace); err == nil {
return replace, nil
}
}
return "", err
}
}
// IsAll joins a set of validators methods and returns true if ALL of them match
func IsAll(v ...ParseMethod) ParseMethod {
return func(name, value string) (string, error) {
var err error
replace := value
for _, c := range v {
if replace, err = c(name, replace); err != nil {
return "", err
}
}
return replace, nil
}
}
var rxIsInt = regexp.MustCompile(`^[1-9][0-9]*$`)
// IsInt checks if value is an integer
func IsInt(name, value string) (string, error) {
if !rxIsInt.MatchString(value) {
return "", fmt.Errorf("Is not integer")
}
return value, nil
}
var rxIsFloat = regexp.MustCompile(`^[0-9]+(?:\.[0-9]+)?$`)
// IsFloat checks if value is float
func IsFloat(name, value string) (string, error) {
if !rxIsFloat.MatchString(value) {
return "", fmt.Errorf("Is not float")
}
return value, nil
}