Skip to content

Commit b074012

Browse files
committed
⚡ up: performance apply the "required" validator
1 parent 66dd220 commit b074012

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

validating.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,15 @@ func (r *Rule) valueValidate(field, name string, val any, v *Validation) (ok boo
248248
return true
249249
}
250250

251-
// call custom validator in the rule.
251+
// support check sub element in a slice list. eg: field=top.user.*.name
252+
dotStarNum := strings.Count(field, ".*")
253+
254+
// perf: The most commonly used rule "required" - direct call v.Required()
255+
if name == RuleRequired && dotStarNum == 0 {
256+
return v.Required(field, val)
257+
}
258+
259+
// call value validator in the rule.
252260
fm := r.checkFuncMeta
253261
if fm == nil {
254262
// fallback: get validator from global or validation
@@ -279,8 +287,6 @@ func (r *Rule) valueValidate(field, name string, val any, v *Validation) (ok boo
279287
rftVal := reflect.ValueOf(val)
280288
valKind := rftVal.Kind()
281289

282-
// feat: support check sub element in a slice list. eg: field=top.user.*.name
283-
dotStarNum := strings.Count(field, ".*")
284290
if valKind == reflect.Slice && dotStarNum > 0 {
285291
sliceLen, sliceCap := rftVal.Len(), rftVal.Cap()
286292

validation_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -717,9 +717,11 @@ func TestValidation_ValidateData(t *testing.T) {
717717

718718
ok := v.ValidateData(d)
719719
assert.True(t, ok)
720+
assert.NotEmpty(t, v.SafeData())
720721

721722
v.Reset()
722-
assert.Len(t, v.Validators(false), 0)
723+
assert.Empty(t, v.SafeData())
724+
assert.Empty(t, v.FilteredData())
723725
}
724726

725727
func TestGetSet_OnNilData(t *testing.T) {

0 commit comments

Comments
 (0)