Skip to content

Commit

Permalink
fix toString converter (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
slipros authored Feb 20, 2024
1 parent 0f0508e commit 15fd925
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
17 changes: 9 additions & 8 deletions basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ func valueIsEmpty(value reflect.Value, allowZeroValue bool) bool {
}

func toString(v any) (string, bool) {
str, ok := v.(string)
if !ok {
i, ok := v.(fmt.Stringer)
if !ok {
return "", false
}
switch v := v.(type) {
case string:
return v, true
case *string:
return *v, true
}

str = i.String()
if i, ok := v.(fmt.Stringer); ok {
return i.String(), true
}

return str, true
return "", false
}
6 changes: 3 additions & 3 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ func (v *ValidationError) WithValuePath(valuePath []string) *ValidationError {
return v
}

// ErrorMessagesIndexedByPath - проверяет на ошибку валидации и возвращает аттрибуты, где ключ равняется полю, а значения ошибкам валидации.
// IsError - проверяет на ошибку валидации и возвращает аттрибуты, где ключ равняется полю, а значения ошибкам валидации.
//
// {
// "client_id": [
// "Value cannot be blank.",
// "Value is invalid."
// ]
// }
func ErrorMessagesIndexedByPath(err error) (map[string][]string, bool) {
var result *Result
func IsError(err error) (map[string][]string, bool) {
var result Result
if errors.As(err, &result) {
return result.ErrorMessagesIndexedByPath(), true
}
Expand Down
6 changes: 5 additions & 1 deletion nested_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func TestNested_ValidateValue(t *testing.T) {
err := Validate(ctx, &obj, rules)
assert.Error(t, err)

var result Result
assert.ErrorAs(t, err, &result)

expectedError := Result{errors: []*ValidationError{
{
Message: "Value must be no less than 2.",
Expand All @@ -62,7 +65,8 @@ func TestNested_ValidateValue(t *testing.T) {
ValuePath: []string{"each", "1"},
},
}}
assert.Equal(t, expectedError, err)

assert.Equal(t, expectedError, result)

errorMessages := err.(Result).ErrorMessagesIndexedByPath()
expectedMessages := map[string][]string{
Expand Down
22 changes: 22 additions & 0 deletions uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,34 @@ import (
"github.com/stretchr/testify/assert"
)

type uuidStringer struct {
Value string
}

func (s uuidStringer) String() string {
return s.Value
}

func TestUUID_ValidateValue_Successfully(t *testing.T) {
ctx := context.Background()
err := NewUUID().ValidateValue(ctx, "00000000-0000-0000-0000-000000000001")
assert.NoError(t, err)
}

func TestUUID_ValidatePtrValue_Successfully(t *testing.T) {
ctx := context.Background()
value := "00000000-0000-0000-0000-000000000001"
err := NewUUID().ValidateValue(ctx, &value)
assert.NoError(t, err)
}

func TestUUID_ValidateStringerValue_Successfully(t *testing.T) {
ctx := context.Background()
value := uuidStringer{Value: "00000000-0000-0000-0000-000000000001"}
err := NewUUID().ValidateValue(ctx, &value)
assert.NoError(t, err)
}

func TestUUID_ValidateValue_InvalidValue_Failed(t *testing.T) {
ctx := context.Background()
err := NewUUID().ValidateValue(ctx, "12323435-343")
Expand Down

0 comments on commit 15fd925

Please sign in to comment.