-
Notifications
You must be signed in to change notification settings - Fork 3
/
extravalidations.go
44 lines (41 loc) · 1016 Bytes
/
extravalidations.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
package webui
import (
"github.com/go-macaron/binding"
"strings"
)
func init() {
// UIntegerNotZero
binding.AddRule(&binding.Rule{
IsMatch: func(rule string) bool {
return strings.HasPrefix(rule, "IntegerNotZero")
},
IsValid: func(errs binding.Errors, name string, v interface{}) (bool, binding.Errors) {
num, ok := v.(int)
if !ok {
return false, errs
}
if num < 0 {
errs.Add([]string{name}, "IntegerNotZero", "Value should be greater than zero")
return false, errs
}
return true, errs
},
})
//UIntegerAndLessOne
binding.AddRule(&binding.Rule{
IsMatch: func(rule string) bool {
return strings.HasPrefix(rule, "UIntegerAndLessOne")
},
IsValid: func(errs binding.Errors, name string, v interface{}) (bool, binding.Errors) {
num, ok := v.(int)
if !ok {
return false, errs
}
if num < -1 {
errs.Add([]string{name}, "UIntegerAndLessOne", "Value should be a positive integer or -1")
return false, errs
}
return true, errs
},
})
}