Skip to content

Commit

Permalink
Fix required and human validators
Browse files Browse the repository at this point in the history
  • Loading branch information
raoptimus committed Mar 25, 2024
1 parent 44a6647 commit 95efc73
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 34 deletions.
8 changes: 5 additions & 3 deletions basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func indirectValue(v any) (value any, isValid bool) {
return val.Interface(), true
}

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

Expand All @@ -40,7 +40,9 @@ func valueIsEmpty(value reflect.Value, allowZeroValue bool) bool {
return true
}

return valueIsEmpty(value.Elem(), allowZeroValue)
if isDeref {
return valueIsEmpty(value.Elem(), false)
}
}

return false
Expand Down
2 changes: 1 addition & 1 deletion human_text.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package validator

const humanRegexp = `^[А-Яа-яЁёa-zA-Z0-9 ,.-]+$`
const humanRegexp = "^[\\p{L}\\d !?-~–—‘.,'\"«»„“’`´′″\\[\\]\\/]+$"

type HumanText struct {
*MatchRegularExpression
Expand Down
15 changes: 6 additions & 9 deletions required.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import (
)

type Required struct {
message string
allowZeroValue bool
whenFunc WhenFunc
skipError bool
message string
whenFunc WhenFunc
skipError bool
}

func NewRequired() *Required {
return &Required{
message: "Value cannot be blank.",
allowZeroValue: false,
message: "Value cannot be blank.",
}
}

Expand All @@ -41,9 +39,8 @@ func (r *Required) WithMessage(message string) *Required {
return &rc
}

// deprecated: should be removed
func (r *Required) WithAllowZeroValue() *Required {
r.allowZeroValue = true

return r
}

Expand All @@ -63,7 +60,7 @@ func (r *Required) setSkipOnError(v bool) {

func (r *Required) ValidateValue(_ context.Context, value any) error {
v := reflect.ValueOf(value)
if valueIsEmpty(v, r.allowZeroValue) {
if valueIsEmpty(v, false) {
return NewResult().WithError(NewValidationError(r.message))
}

Expand Down
2 changes: 1 addition & 1 deletion string_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (r *StringLength) ValidateValue(_ context.Context, value any) error {
}

result := NewResult()
v = strings.Trim(v, " ")
v = strings.TrimSpace(v)
l := utf8.RuneCountInString(v)

if l < r.min {
Expand Down
2 changes: 1 addition & 1 deletion validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func normalizeRules(rules []Rule) []Rule {

func isSkipValidate(ctx context.Context, value any, r Rule) bool {
if rse, ok := r.(RuleSkipEmpty); ok {
if rse.skipOnEmpty() && valueIsEmpty(reflect.ValueOf(value), false) {
if rse.skipOnEmpty() && valueIsEmpty(reflect.ValueOf(value), true) {
return true
}
}
Expand Down
21 changes: 2 additions & 19 deletions validator_required_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestValidatorRequired_NilPointerValue_ReturnsExpectedError(t *testing.T) {
assert.NotNil(t, err)
}

func TestValidatorRequired_EmptyPointerValue_ReturnsExpectedError(t *testing.T) {
func TestValidatorRequired_EmptyPointerValue_Successfully(t *testing.T) {
ctx := context.Background()
v := ""
dto := &testObject2{Name: &v}
Expand All @@ -68,24 +68,7 @@ func TestValidatorRequired_EmptyPointerValue_ReturnsExpectedError(t *testing.T)
},
}
err := Validate(ctx, dto, rules)
assert.Error(t, err)
assert.Equal(t, "Name: Required.", err.Error())
assert.Equal(t, map[string][]string{"Name": {"Required"}}, err.(Result).ErrorMessagesIndexedByPath())
}

func TestValidatorRequired_EmptyPointerValueWithJsonTag_ReturnsExpectedError(t *testing.T) {
ctx := context.Background()
v := ""
dto := &testObject3{Name: &v}
rules := RuleSet{
"Name": {
NewRequired().WithMessage("Required"),
},
}
err := Validate(ctx, dto, rules)
assert.NotNil(t, err)
assert.Equal(t, "name: Required.", err.Error())
assert.Equal(t, map[string][]string{"name": {"Required"}}, err.(Result).ErrorMessagesIndexedByPath())
assert.NoError(t, err)
}

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

0 comments on commit 95efc73

Please sign in to comment.