This repository has been archived by the owner on Dec 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DigitValidator.go
63 lines (57 loc) · 1.98 KB
/
DigitValidator.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
62
63
package ecms_validator
import (
"fmt"
"reflect"
"regexp"
)
var (
digitRegex *regexp.Regexp
DigitValidatorMessageFuncs MessageFuncs
)
func init () {
digitRegex = regexp.MustCompile("^\\d+$")
DigitValidatorMessageFuncs = MessageFuncs{
DoesNotMatchPattern: func(options ValidatorOptions, x interface{}) string {
ops := options.(RegexValidatorOptions)
return fmt.Sprintf("%v contains non-digital characters; Received: %v", x, ops.Pattern.String())
},
}
}
func newDigitValidatorOptions () RegexValidatorOptions {
return RegexValidatorOptions{
Pattern: digitRegex,
MessageFuncs: &DigitValidatorMessageFuncs,
}
}
// DigitValidator - Returns `(true, nil)` for `uint`, `int`, and strings containing
// only digit characters (numbers). For every other value the returned validator
// will always return `(false, []string{})` where the second return value is
// the error messages returned for current run.
// Note: The `Pattern` property of passed in `RegexValidatorOptions` gets ignored
// and the internally defined one gets used.
func DigitValidator (options RegexValidatorOptions) Validator {
return func (x interface{}) (bool, []string) {
rv := reflect.ValueOf(x)
k := rv.Kind()
switch k {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return true, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if rv.Int() < 0 {
return false, []string{options.GetErrorMessageByKey(DoesNotMatchPattern, x)}
}
return true, nil
case reflect.String:
ops := newDigitValidatorOptions()
ops.MessageFuncs = options.GetMessageFuncs()
return RegexValidator(ops)(x.(string))
default:
return false, []string{options.GetErrorMessageByKey(DoesNotMatchPattern, x)}
}
}
}
// DigitValidator1 - Ignores options param and just returns a validator
// which contains default error messages in message templates
func DigitValidator1 () Validator {
return DigitValidator(newDigitValidatorOptions())
}