fixed validators for number

This commit is contained in:
Shreyas Goenka 2023-05-23 19:25:51 +02:00
parent 4fa8c9c06d
commit 2c02fdf2fb
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
3 changed files with 14 additions and 16 deletions

View File

@ -123,18 +123,17 @@ func TestTemplateSchemaValidateType(t *testing.T) {
// assert validation passing
err := validateType(int(0), PropertyTypeInt)
assert.NoError(t, err)
err = validateType(int32(1), PropertyTypeInt)
assert.NoError(t, err)
err = validateType(int64(1), PropertyTypeInt)
assert.NoError(t, err)
err = validateType(float32(1.1), PropertyTypeNumber)
assert.NoError(t, err)
err = validateType(float64(1.2), PropertyTypeNumber)
assert.NoError(t, err)
err = validateType(int(1), PropertyTypeNumber)
assert.NoError(t, err)
err = validateType(false, PropertyTypeBoolean)
assert.NoError(t, err)
@ -151,8 +150,6 @@ func TestTemplateSchemaValidateType(t *testing.T) {
assert.ErrorContains(t, err, "expected type integer, but value is \"abc\"")
// assert validation failing for floats
err = validateType(int(1), PropertyTypeNumber)
assert.ErrorContains(t, err, "expected type float, but value is 1")
err = validateType(true, PropertyTypeNumber)
assert.ErrorContains(t, err, "expected type float, but value is true")
err = validateType("abc", PropertyTypeNumber)

View File

@ -23,8 +23,8 @@ func validateBoolean(v any) error {
return nil
}
func validateFloat(v any) error {
if !slices.Contains([]reflect.Kind{reflect.Float32, reflect.Float64},
func validateNumber(v any) error {
if !slices.Contains([]reflect.Kind{reflect.Float32, reflect.Float64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64},
reflect.TypeOf(v).Kind()) {
return fmt.Errorf("expected type float, but value is %#v", v)
}
@ -43,5 +43,5 @@ var validators map[PropertyType]Validator = map[PropertyType]Validator{
PropertyTypeString: validateString,
PropertyTypeBoolean: validateBoolean,
PropertyTypeInt: validateInteger,
PropertyTypeNumber: validateFloat,
PropertyTypeNumber: validateNumber,
}

View File

@ -4,6 +4,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestValidatorString(t *testing.T) {
@ -35,22 +36,22 @@ func TestValidatorBoolean(t *testing.T) {
}
func TestValidatorFloat(t *testing.T) {
err := validateFloat(true)
err := validateNumber(true)
assert.ErrorContains(t, err, "expected type float, but value is true")
err = validateFloat(int32(1))
assert.ErrorContains(t, err, "expected type float, but value is 1")
err = validateNumber(int32(1))
require.NoError(t, err)
err = validateFloat(int64(1))
assert.ErrorContains(t, err, "expected type float, but value is 1")
err = validateNumber(int64(1))
require.NoError(t, err)
err = validateFloat(float32(1))
err = validateNumber(float32(1))
assert.NoError(t, err)
err = validateFloat(float64(1))
err = validateNumber(float64(1))
assert.NoError(t, err)
err = validateFloat("abc")
err = validateNumber("abc")
assert.ErrorContains(t, err, "expected type float, but value is \"abc\"")
}