Skip to content

Commit

Permalink
⚡ up: performance apply the "required" validator
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 1, 2023
1 parent 66dd220 commit b074012
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 9 additions & 3 deletions validating.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,15 @@ func (r *Rule) valueValidate(field, name string, val any, v *Validation) (ok boo
return true
}

// call custom validator in the rule.
// support check sub element in a slice list. eg: field=top.user.*.name
dotStarNum := strings.Count(field, ".*")

// perf: The most commonly used rule "required" - direct call v.Required()
if name == RuleRequired && dotStarNum == 0 {
return v.Required(field, val)
}

// call value validator in the rule.
fm := r.checkFuncMeta
if fm == nil {
// fallback: get validator from global or validation
Expand Down Expand Up @@ -279,8 +287,6 @@ func (r *Rule) valueValidate(field, name string, val any, v *Validation) (ok boo
rftVal := reflect.ValueOf(val)
valKind := rftVal.Kind()

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

Expand Down
4 changes: 3 additions & 1 deletion validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,9 +717,11 @@ func TestValidation_ValidateData(t *testing.T) {

ok := v.ValidateData(d)
assert.True(t, ok)
assert.NotEmpty(t, v.SafeData())

v.Reset()
assert.Len(t, v.Validators(false), 0)
assert.Empty(t, v.SafeData())
assert.Empty(t, v.FilteredData())
}

func TestGetSet_OnNilData(t *testing.T) {
Expand Down

0 comments on commit b074012

Please sign in to comment.