-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation_rules.go
More file actions
54 lines (46 loc) · 1023 Bytes
/
validation_rules.go
File metadata and controls
54 lines (46 loc) · 1023 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
48
49
50
51
52
53
54
package validation
import (
"strings"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/pkg/errors"
)
type DynamicRule struct {
RuleKey string
ValidationRules map[string][]validation.Rule
FormatData func(value string) string
}
func (d DynamicRule) Get() []validation.Rule {
return d.ValidationRules[d.RuleKey]
}
type validateRule struct {
rule func() error
}
func WithRule(rule func() error) validation.Rule {
return &validateRule{rule: rule}
}
func (l *validateRule) Validate(value interface{}) error {
return l.rule()
}
func validateInRule(equalsIgnoreCase bool, values ...string) RuleFunc {
return func(value interface{}) error {
valStr, ok := value.(string)
if !ok {
return errors.New("must be a string")
}
if valStr == "" {
return nil
}
for _, m := range values {
if equalsIgnoreCase {
if strings.EqualFold(m, valStr) {
return nil
}
} else {
if m == valStr {
return nil
}
}
}
return errors.New("invalid value")
}
}