-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruleexpr.go
More file actions
172 lines (160 loc) · 4.74 KB
/
Copy pathruleexpr.go
File metadata and controls
172 lines (160 loc) · 4.74 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package validator
import (
"errors"
"fmt"
"maps"
"strings"
"github.com/libtnb/validator/internal/dsl"
)
// AddRules appends expressions to field's rules, joined with &&. A top-level
// || on either side is parenthesized so precedence is preserved, and when
// the existing expression dives, the new rules apply to the container, before
// dive. Empty strings are skipped. The candidate is compiled first: an
// invalid expression returns its parse error and leaves the field's rules
// unchanged. A field unknown to the input is simply validated as absent.
//
// Returns ErrValidated after Validate has run.
func (vd *Validation) AddRules(field string, rules ...string) error {
if err := vd.ensureMutable(); err != nil {
return err
}
candidate := vd.rules[field]
for _, r := range rules {
if strings.TrimSpace(r) == "" {
continue
}
candidate = vd.mergeRule(candidate, r)
}
if candidate == vd.rules[field] {
return nil
}
if err := vd.checkExpr(candidate); err != nil {
return err
}
vd.ensureOwnedRules()
vd.rules[field] = candidate
return nil
}
// RemoveRules deletes the named rules from the top-level && chain of field's
// expression; a name may also be a full segment such as "min:3". Rules
// nested under ||, ! or parentheses, and "dive" itself, are never removed.
// An unknown field or no names is a no-op; a field whose expression becomes
// empty loses its entry.
//
// Returns ErrValidated after Validate has run.
func (vd *Validation) RemoveRules(field string, rules ...string) error {
if err := vd.ensureMutable(); err != nil {
return err
}
if len(rules) == 0 {
return nil
}
cur, ok := vd.rules[field]
if !ok {
return nil
}
names := make(map[string]bool, len(rules))
for _, r := range rules {
names[strings.TrimSpace(r)] = true
}
vd.ensureOwnedRules()
if nw := dsl.RemoveTopLevelLeaves(cur, names, vd.validator.isRawArg); strings.TrimSpace(nw) == "" {
delete(vd.rules, field)
} else {
vd.rules[field] = nw
}
return nil
}
// ClearRules drops field's entire expression, dive rules included; an unknown
// field is a no-op.
//
// Returns ErrValidated after Validate has run.
func (vd *Validation) ClearRules(field string) error {
if err := vd.ensureMutable(); err != nil {
return err
}
if _, ok := vd.rules[field]; ok {
vd.ensureOwnedRules()
delete(vd.rules, field)
}
return nil
}
// Rules returns a copy of the current field -> expression map, reflecting
// AddRules, RemoveRules and ClearRules. The map is never nil and modifying
// it has no effect on the Validation.
func (vd *Validation) Rules() map[string]string { return copyStrMap(vd.rules) }
// ensureOwnedRules copy-on-writes vd.rules before mutation: never corrupt the
// shared cached map, and drop the plan compiled for it.
func (vd *Validation) ensureOwnedRules() {
if vd.rulesShared || vd.rules == nil {
vd.rules = copyStrMap(vd.rules)
vd.rulesShared = false
vd.plan = nil
}
}
// mergeRule adds r to the container side of a dive expression.
func (vd *Validation) mergeRule(existing, r string) string {
if strings.TrimSpace(existing) == "" {
return r
}
if ds := vd.validator.splitDive(existing); ds.err == nil && ds.hasDive {
// element is never empty here: SplitDive rejects a trailing dive.
container := r
if ds.container != "" {
container = vd.joinAnd(ds.container, r)
}
return container + " && dive && " + ds.element
}
return vd.joinAnd(existing, r)
}
func (vd *Validation) checkExpr(expr string) error {
ds := vd.validator.splitDive(expr)
if ds.err != nil {
return ds.err
}
if ds.nested {
return fmt.Errorf("validator: multiple top-level 'dive' is not supported")
}
if strings.TrimSpace(ds.container) != "" {
if _, err := vd.validator.compile(ds.container); err != nil {
return err
}
}
if ds.hasDive && strings.TrimSpace(ds.element) != "" {
e, err := vd.validator.compile(ds.element)
if err != nil {
return err
}
if e.sometimes {
return errors.New(errSometimesInDive)
}
}
return nil
}
// joinAnd ANDs existing and r, parenthesizing a top-level || side so && (tighter) doesn't capture it.
func (vd *Validation) joinAnd(existing, r string) string {
if dsl.HasTopLevelOr(existing, vd.validator.isRawArg) {
existing = "(" + escapeTrailingBackslash(existing) + ")"
}
if dsl.HasTopLevelOr(r, vd.validator.isRawArg) {
r = "(" + escapeTrailingBackslash(r) + ")"
}
return existing + " && " + r
}
// escapeTrailingBackslash doubles a trailing odd backslash run so a following
// structural ")" can't be swallowed as an escape.
func escapeTrailingBackslash(s string) string {
var n int
for i := len(s) - 1; i >= 0 && s[i] == '\\'; i-- {
n++
}
if n%2 == 1 {
return s + "\\"
}
return s
}
func copyStrMap(m map[string]string) map[string]string {
out := make(map[string]string, len(m))
maps.Copy(out, m)
return out
}