diff --git a/helper/any.go b/helper/any.go index 9ea8d28..5066a5a 100644 --- a/helper/any.go +++ b/helper/any.go @@ -63,7 +63,7 @@ func AnyToString(v any) (string, error) { uint, uint8, uint16, uint32, uint64: return fmt.Sprintf("%d", v), nil case float32, float64: - return fmt.Sprintf("%f", v), nil + return fmt.Sprintf("%v", v), nil default: return "", fmt.Errorf("unsupported type for value: %T", v) } diff --git a/helper/any_test.go b/helper/any_test.go index 215f9de..6450b33 100644 --- a/helper/any_test.go +++ b/helper/any_test.go @@ -166,7 +166,7 @@ func TestAnyToString(t *testing.T) { { name: "Valid float32", arg: float32(1), - expected: "1.000000", + expected: "1", expectedError: false, }, { @@ -229,7 +229,7 @@ func TestAnyToArrayOfString(t *testing.T) { { name: "Valid array float32", arg: []float32{1}, - expected: []string{"1.000000"}, + expected: []string{"1"}, expectedError: false, }, { diff --git a/validator.go b/validator.go index 1d80510..df1e2bd 100644 --- a/validator.go +++ b/validator.go @@ -148,11 +148,20 @@ func (r *Validator) ValidateWithValidation(jsonInput map[string]any, validations continue } } - if validation.OmitEmpty && jsonValue == "" { continue } + if jsonValue != nil { + switch validation.Type { + case model.String, model.Int, model.Float, model.Bool: + coerced, coerceErr := helper.AnyToType(jsonValue, validation.Type.ToReflectType()) + if coerceErr == nil { + jsonValue = coerced + } + } + } + var err error switch validation.Type { case model.Struct: diff --git a/validator_test.go b/validator_test.go index 40ef2bd..e720af0 100644 --- a/validator_test.go +++ b/validator_test.go @@ -343,7 +343,7 @@ func TestValidateWithValidation(t *testing.T) { }, }, }, - expected: map[string]any{"isActive": "false"}, + expected: map[string]any{"isActive": false}, wantErr: false, }, {