-
Notifications
You must be signed in to change notification settings - Fork 3
/
or.go
81 lines (63 loc) · 1.14 KB
/
or.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package validator
import (
"context"
)
type OR struct {
rules []Rule
message string
whenFunc WhenFunc
skipEmpty bool
skipError bool
}
func NewOR(message string, rules ...Rule) *OR {
return &OR{
message: message,
rules: rules,
}
}
func (o *OR) WithMessage(message string) *OR {
rc := *o
rc.message = message
return &rc
}
func (o *OR) When(v WhenFunc) *OR {
rc := *o
rc.whenFunc = v
return &rc
}
func (o *OR) when() WhenFunc {
return o.whenFunc
}
func (o *OR) setWhen(v WhenFunc) {
o.whenFunc = v
}
func (o *OR) SkipOnEmpty() *OR {
rc := *o
rc.skipEmpty = true
return &rc
}
func (o *OR) skipOnEmpty() bool {
return o.skipEmpty
}
func (o *OR) setSkipOnEmpty(v bool) {
o.skipEmpty = v
}
func (o *OR) SkipOnError() *OR {
rs := *o
rs.skipError = true
return &rs
}
func (o *OR) shouldSkipOnError() bool {
return o.skipError
}
func (o *OR) setSkipOnError(v bool) {
o.skipError = v
}
func (o *OR) ValidateValue(ctx context.Context, value any) error {
for _, r := range o.rules {
if err := r.ValidateValue(ctx, value); err == nil {
return nil
}
}
return NewResult().WithError(NewValidationError(o.message))
}