Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion helper/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions helper/any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func TestAnyToString(t *testing.T) {
{
name: "Valid float32",
arg: float32(1),
expected: "1.000000",
expected: "1",
expectedError: false,
},
{
Expand Down Expand Up @@ -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,
},
{
Expand Down
11 changes: 10 additions & 1 deletion validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func TestValidateWithValidation(t *testing.T) {
},
},
},
expected: map[string]any{"isActive": "false"},
expected: map[string]any{"isActive": false},
wantErr: false,
},
{
Expand Down
Loading