-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.go
More file actions
244 lines (220 loc) · 8 KB
/
Copy pathvalidator.go
File metadata and controls
244 lines (220 loc) · 8 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package confx
import (
"context"
"fmt"
"reflect"
"regexp"
"strings"
"github.com/go-playground/validator/v10"
"github.com/pkg/errors"
"github.com/samber/lo"
"github.com/spf13/cast"
)
var reSplitParams = regexp.MustCompile(`'[^']*'|\S+`)
// parseOneOfParam2 parses a string that contains multiple values separated by
// spaces and/or single quotes. The single quotes are used to enclose values
// that contain spaces.
//
// Examples:
// - "a b c" -> ["a", "b", "c"]
// - "'a b' c" -> ["a b", "c"]
// - "'a b' 'c d'" -> ["a b", "c d"]
// - "'a b c'" -> ["a b c"]
func parseOneOfParam2(s string) []string {
vals := reSplitParams.FindAllString(s, -1)
for i := 0; i < len(vals); i++ {
vals[i] = strings.ReplaceAll(vals[i], "'", "")
}
return vals
}
func requireCheckFieldValue(
fl validator.FieldLevel, param string, value string, defaultNotFoundValue bool,
) bool {
field, kind, _, found := fl.GetStructFieldOKAdvanced2(fl.Parent(), param)
if !found {
return defaultNotFoundValue
}
switch kind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return field.Int() == cast.ToInt64(value)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return field.Uint() == cast.ToUint64(value)
case reflect.Float32:
return field.Float() == float64(cast.ToFloat32(value))
case reflect.Float64:
return field.Float() == cast.ToFloat64(value)
case reflect.Slice, reflect.Map, reflect.Array:
return int64(field.Len()) == cast.ToInt64(value)
case reflect.Bool:
return field.Bool() == (value == "true")
case reflect.Ptr:
if field.IsNil() {
return value == "nil"
}
// Handle non-nil pointers
return requireCheckFieldValue(fl, param, value, defaultNotFoundValue)
default:
// default reflect.String:
return field.String() == value
}
}
type Validator interface {
RegisterValidationCtx(tag string, fn validator.FuncCtx, callValidationEvenIfNull ...bool) error
StructCtx(ctx context.Context, v any) error
}
type ValidatorFunc func(ctx context.Context, v any) error
type wrappedValidator struct {
Validator
structCtxFunc ValidatorFunc
}
func (w *wrappedValidator) StructCtx(ctx context.Context, v any) error {
return w.structCtxFunc(ctx, v)
}
const (
// stopIfTag / stopUnlessTag stop validation of a field at that point.
stopIfTag = "stop_if"
stopUnlessTag = "stop_unless"
// skipNestedUnlessTag is the original name of stop_unless, kept as an alias
// so existing struct tags keep working.
//
// Deprecated: use stop_unless. The "nested" is misleading — the tag is not
// specific to nested structs (see the note on stopUnlessImpl).
skipNestedUnlessTag = "skip_nested_unless"
)
// stopTagImpls are registered together by ValidatorWithSkipNestedUnless.
var stopTagImpls = map[string]validator.FuncCtx{
stopIfTag: stopIfImpl,
stopUnlessTag: stopUnlessImpl,
skipNestedUnlessTag: stopUnlessImpl,
}
// stopTags are the tags whose "failure" means "stop validating here", not
// "this field is invalid". Their errors are filtered out after StructCtx.
var stopTags = []string{stopIfTag, stopUnlessTag, skipNestedUnlessTag}
// stopUnlessImpl stops validating a field unless every (field, value) pair
// matches. It backs the "stop_unless" tag and its "skip_nested_unless" alias.
//
// stopIfImpl is the same thing with the opposite polarity. Polarity is the ONLY
// difference between the two; see the note there for what "stop" covers.
//
// type Config struct {
// Type string `validate:"oneof=local remote"`
// Local LocalConf `validate:"stop_unless=Type local"`
// Remote RemoteConf `validate:"stop_unless=Type remote"`
// }
//
// Local is validated only when Type == "local", Remote only when Type ==
// "remote". All pairs must match for validation to proceed.
//
// Panics if the number of parameters is not even.
func stopUnlessImpl(_ context.Context, fl validator.FieldLevel) bool {
params := parseOneOfParam2(fl.Param())
if len(params)%2 != 0 {
panic(fmt.Sprintf("Bad param number for %s %s", fl.GetTag(), fl.FieldName()))
}
for i := 0; i < len(params); i += 2 {
// Returning false is how validation is stopped: it produces an error
// that the wrapper then filters out by tag name (see stopTags).
if !requireCheckFieldValue(fl, params[i], params[i+1], false) {
return false
}
}
return true
}
// stopIfImpl stops validating a field when ANY (field, value) pair matches. It
// backs the "stop_if" tag.
//
// "stop" rather than "skip", because what it stops depends on where the tag
// sits, and both are the same underlying behaviour — validator abandons a field
// at its first failing tag:
//
// - on a scalar field, the tags AFTER it do not run;
// - on a nested struct, validation does not descend into it.
//
// Put it first in the tag list. Parameters are pairs of (field name, value).
//
// The motivating case is a cross-field comparison whose right-hand side carries
// a sentinel. `ltefield=MaxOpenConns` reads as "at most MaxOpenConns", but when
// MaxOpenConns is 0 meaning UNLIMITED it is not an upper bound at all, and the
// tag rejects a perfectly good config:
//
// MaxIdleConns int `validate:"stop_if=MaxOpenConns 0,ltefield=MaxOpenConns"`
// MaxOpenConns int // 0 = unlimited
//
// Not to be confused with validator's built-in "skip_unless", which despite its
// name never skips anything: it returns hasValue(fl), a presence check in the
// required_* family, and the tags after it still run. No built-in stops
// validation the way these do, which is why they exist.
//
// The names deliberately stay out of the upstream "skip_*" namespace.
// RegisterValidationCtx silently REPLACES a built-in of the same name and
// returns nil, so a collision would change behaviour for every consumer with
// nothing to announce it.
//
// Panics if the number of parameters is not even.
func stopIfImpl(_ context.Context, fl validator.FieldLevel) bool {
params := parseOneOfParam2(fl.Param())
if len(params)%2 != 0 {
panic(fmt.Sprintf("Bad param number for %s %s", fl.GetTag(), fl.FieldName()))
}
for i := 0; i < len(params); i += 2 {
// A missing field is not a match, so a typo'd field name never silently
// disables the rules that follow.
if requireCheckFieldValue(fl, params[i], params[i+1], false) {
return false
}
}
return true
}
// stopTagsWrapper strips the errors produced by the stop tags. They fail on
// purpose — that is how validation is halted — so their errors are an
// implementation detail and must never reach the caller.
func stopTagsWrapper(next ValidatorFunc) ValidatorFunc {
return func(ctx context.Context, v any) error {
err := next(ctx, v)
if err == nil {
return nil
}
var verr validator.ValidationErrors
if errors.As(err, &verr) {
filtered := lo.Filter(verr, func(e validator.FieldError, _ int) bool {
return !lo.Contains(stopTags, e.Tag())
})
if len(filtered) == 0 {
return nil
}
return filtered
}
return err
}
}
// ValidatorWithSkipNestedUnless wraps a validator with support for the
// conditional "stop" tags, which halt validation of a field based on the values
// of other fields in the same struct.
//
// The wrapper performs two functions:
// 1. Registers "stop_if", "stop_unless", and "skip_nested_unless" (a
// deprecated alias of stop_unless, kept so existing tags keep working)
// 2. Filters out their errors, which mean "stop validating here", not "this
// value is invalid"
//
// The name is historical — it predates stop_if/stop_unless — and is kept
// because it is part of the public API.
//
// Parameters:
// - validator: The base validator to wrap
//
// Returns:
// - Validator: A wrapped validator supporting the stop tags
//
// Panics if registration of any tag fails
func ValidatorWithSkipNestedUnless(validator Validator) Validator {
for tag, impl := range stopTagImpls {
if err := validator.RegisterValidationCtx(tag, impl); err != nil {
panic(fmt.Sprintf("failed to register validation %q: %v", tag, err))
}
}
return &wrappedValidator{
Validator: validator,
structCtxFunc: stopTagsWrapper(validator.StructCtx),
}
}