-
Notifications
You must be signed in to change notification settings - Fork 3
/
match_regular_expression.go
98 lines (75 loc) · 1.8 KB
/
match_regular_expression.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package validator
import (
"context"
"github.com/raoptimus/validator.go/v2/regexpc"
)
type MatchRegularExpression struct {
message string
pattern string
whenFunc WhenFunc
skipEmpty bool
skipError bool
}
func NewMatchRegularExpression(pattern string) *MatchRegularExpression {
return &MatchRegularExpression{
message: "Value is invalid.",
pattern: pattern,
}
}
func (r *MatchRegularExpression) WithPattern(pattern string) *MatchRegularExpression {
rc := *r
rc.pattern = pattern
return &rc
}
func (r *MatchRegularExpression) WithMessage(message string) *MatchRegularExpression {
rc := *r
rc.message = message
return &rc
}
func (r *MatchRegularExpression) When(v WhenFunc) *MatchRegularExpression {
rc := *r
rc.whenFunc = v
return &rc
}
func (r *MatchRegularExpression) when() WhenFunc {
return r.whenFunc
}
func (r *MatchRegularExpression) setWhen(v WhenFunc) {
r.whenFunc = v
}
func (r *MatchRegularExpression) SkipOnEmpty() *MatchRegularExpression {
rc := *r
rc.skipEmpty = true
return &rc
}
func (r *MatchRegularExpression) skipOnEmpty() bool {
return r.skipEmpty
}
func (r *MatchRegularExpression) setSkipOnEmpty(v bool) {
r.skipEmpty = v
}
func (r *MatchRegularExpression) SkipOnError() *MatchRegularExpression {
rs := *r
rs.skipError = true
return &rs
}
func (r *MatchRegularExpression) shouldSkipOnError() bool {
return r.skipError
}
func (r *MatchRegularExpression) setSkipOnError(v bool) {
r.skipError = v
}
func (r *MatchRegularExpression) ValidateValue(_ context.Context, value any) error {
v, ok := toString(value)
if !ok {
return NewResult().WithError(NewValidationError(r.message))
}
rg, err := regexpc.Compile(r.pattern)
if err != nil {
return err
}
if !rg.MatchString(v) {
return NewResult().WithError(NewValidationError(r.message))
}
return nil
}