Skip to content

Commit

Permalink
Correction of logic for determining empty value
Browse files Browse the repository at this point in the history
  • Loading branch information
Кирилл Маликов committed Sep 10, 2024
1 parent 534faf6 commit 8876e2d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
27 changes: 10 additions & 17 deletions basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,22 @@ func indirectValue(v any) (value any, isValid bool) {
}

func valueIsEmpty(value reflect.Value) bool {
if !value.IsValid() || value.IsZero() {
if !value.IsValid() {
return true
}

kind := value.Kind()
switch kind {
switch value.Kind() {
case reflect.Slice, reflect.Map:
return value.IsNil() || value.Len() == 0
case reflect.Array, reflect.Struct:
return value.IsZero()
case reflect.String:
if len(strings.TrimSpace(value.String())) == 0 {
return true
}
case reflect.Slice:
if value.Len() == 0 {
return true
}
return len(strings.TrimSpace(value.String())) == 0
case reflect.Ptr:
if value.IsNil() {
return true
}

return valueIsEmpty(value.Elem())
return value.IsNil() || valueIsEmpty(value.Elem())
default:
return false
}

return false
}

func toString(v any) (string, bool) {
Expand Down
3 changes: 1 addition & 2 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ func TestValidateValue_Int_Failure(t *testing.T) {

err := ValidateValue(ctx, 0, rules...)
assert.Error(t, err)
assert.Equal(t, "Value cannot be blank. Value must be no less than 1.", err.Error())
assert.Equal(t, "Value must be no less than 1.", err.Error())

assert.ErrorAs(t, err, &Result{})
expectedResult := NewResult().
WithError(
NewValidationError("Value cannot be blank."),
NewValidationError("Value must be no less than 1.").
WithParams(map[string]any{"min": int64(1), "max": int64(3)}),
)
Expand Down

0 comments on commit 8876e2d

Please sign in to comment.