Skip to content

Commit 66dd220

Browse files
committed
👔 up: update some logic for create new validation, format some codes
1 parent 3142ae2 commit 66dd220

File tree

6 files changed

+105
-92
lines changed

6 files changed

+105
-92
lines changed

register.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import "reflect"
55
var (
66
// global validators. contains built-in and user custom
77
validators map[string]int8
8-
// all validators func meta information
8+
// global validators func meta information
99
validatorMetas map[string]*funcMeta
1010
)
1111

@@ -15,12 +15,18 @@ func init() {
1515
validatorMetas = make(map[string]*funcMeta)
1616

1717
for n, fv := range validatorValues {
18-
validators[n] = 1 // built in
18+
validators[n] = validatorTypeBuiltin
1919
validatorMetas[n] = newFuncMeta(n, true, fv)
2020
}
2121
}
2222

23-
// validator func reflect.Value
23+
// some commonly validation rule names.
24+
const (
25+
RuleRequired = "required"
26+
RuleRegexp = "regexp"
27+
)
28+
29+
// validator func reflect.Value map
2430
var validatorValues = map[string]reflect.Value{
2531
// int value
2632
"lt": reflect.ValueOf(Lt),

validate.go

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ type GlobalOption struct {
7676
//
7777
// tag: default TODO
7878
DefaultTag string
79-
// StopOnError If true: An error occurs, it will cease to continue to verify
79+
// StopOnError If true: An error occurs, it will cease to continue to verify. default is True.
8080
StopOnError bool
81-
// SkipOnEmpty Skip check on field not exist or value is empty
81+
// SkipOnEmpty Skip check on field not exist or value is empty. default is True.
8282
SkipOnEmpty bool
8383
// UpdateSource Whether to update source field value, useful for struct validate
8484
UpdateSource bool
@@ -142,18 +142,34 @@ func newGlobalOption() *GlobalOption {
142142
}
143143
}
144144

145+
// pool for validation instance
146+
// var vPool = &sync.Pool{
147+
// New: func() any {
148+
// return newEmpty()
149+
// },
150+
// }
151+
145152
func newValidation(data DataFace) *Validation {
153+
// v := vPool.Get().(*Validation)
154+
// // reset some runtime data
155+
// v.ResetResult()
156+
// v.trans = NewTranslator()
157+
158+
v := newEmpty()
159+
v.data = data
160+
return v
161+
}
162+
163+
func newEmpty() *Validation {
146164
v := &Validation{
147165
Errors: make(Errors),
148-
// add data source on usage
149-
data: data,
150166
// create message translator
151167
// trans: StdTranslator,
152168
trans: NewTranslator(),
153169
// validated data
154170
safeData: make(map[string]any),
155171
// validator names
156-
validators: make(map[string]int8),
172+
validators: make(map[string]int8, 16),
157173
// filtered data
158174
filteredData: make(map[string]any),
159175
// default config
@@ -162,7 +178,7 @@ func newValidation(data DataFace) *Validation {
162178
}
163179

164180
// init build in context validator
165-
v.validatorValues = map[string]reflect.Value{
181+
ctxValidatorMap := map[string]reflect.Value{
166182
"required": reflect.ValueOf(v.Required),
167183
"requiredIf": reflect.ValueOf(v.RequiredIf),
168184
"requiredUnless": reflect.ValueOf(v.RequiredUnless),
@@ -183,22 +199,14 @@ func newValidation(data DataFace) *Validation {
183199
"inMimeTypes": reflect.ValueOf(v.InMimeTypes),
184200
}
185201

186-
v.validatorMetas = make(map[string]*funcMeta)
202+
v.validatorMetas = make(map[string]*funcMeta, len(ctxValidatorMap))
187203

188-
// collect meta info
189-
for n, fv := range v.validatorValues {
190-
v.validators[n] = 1 // built in
204+
// make and collect meta info
205+
for n, fv := range ctxValidatorMap {
206+
v.validators[n] = validatorTypeBuiltin
191207
v.validatorMetas[n] = newFuncMeta(n, true, fv)
192208
}
193209

194-
// v.pool = &sync.Pool{
195-
// New: func() any {
196-
// return &Validation{
197-
// v: v,
198-
// }
199-
// },
200-
// }
201-
202210
return v
203211
}
204212

@@ -207,11 +215,12 @@ func newValidation(data DataFace) *Validation {
207215
*************************************************************/
208216

209217
// New create a Validation instance
210-
// data support:
211-
// - DataFace
212-
// - M/map[string]any
213-
// - SValues/url.Values/map[string][]string
214-
// - struct ptr
218+
//
219+
// data type support:
220+
// - DataFace
221+
// - M/map[string]any
222+
// - SValues/url.Values/map[string][]string
223+
// - struct ptr
215224
func New(data any, scene ...string) *Validation {
216225
switch td := data.(type) {
217226
case DataFace:
@@ -231,7 +240,7 @@ func New(data any, scene ...string) *Validation {
231240
return Struct(data, scene...)
232241
}
233242

234-
// NewWithOptions new Validation with options
243+
// NewWithOptions new Validation with options TODO
235244
// func NewWithOptions(data any, fn func(opt *GlobalOption)) *Validation {
236245
// fn(gOpt)
237246
// return New(data)

validating_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,14 @@ func TestValidate_map_subSlice_1dotStar(t *testing.T) {
352352

353353
s := v.Errors.String()
354354
assert.StrContains(t, s, "top.cpt.*.encounter_uid is required")
355+
356+
// test invalid path
357+
v = Map(mp)
358+
v.StopOnError = false
359+
v.StringRule("top.*.cpt.*.invalid_path", "required")
360+
assert.False(t, v.Validate())
361+
// fmt.Println(v.Errors)
362+
assert.StrContains(t, v.Errors.String(), "required: top.*.cpt.*.invalid_path is required")
355363
}
356364

357365
func TestRequired_MissingParentField(t *testing.T) {
@@ -373,13 +381,15 @@ func TestRequired_MissingParentField(t *testing.T) {
373381

374382
v := Map(m)
375383
v.StopOnError = false
376-
// v.StringRule("coding.*.details", "required")
377-
// v.StringRule("coding.*.details.em", "required")
384+
v.StringRule("coding.*.details", "required")
385+
v.StringRule("coding.*.details.em", "required")
378386
v.StringRule("coding.*.details.cpt.*.encounter_uid", "required")
379-
v.StringRule("coding.*.details.cpt.*.work_item_uid", "required")
387+
// not exist field
388+
v.StringRule("coding.*.details.cpt.*.not_exist_field", "required")
380389
es := v.ValidateE()
381390
assert.False(t, es.Empty())
391+
382392
s := es.String()
383393
assert.StrContains(t, s, "coding.*.details.cpt.*.encounter_uid is required")
384-
assert.StrContains(t, s, "coding.*.details.cpt.*.work_item_uid is required")
394+
assert.StrContains(t, s, "coding.*.details.cpt.*.not_exist_field is required")
385395
}

0 commit comments

Comments
 (0)