From 2c02fdf2fbd2c79785200c513ac46d1df21c3823 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Tue, 23 May 2023 19:25:51 +0200 Subject: [PATCH] fixed validators for number --- libs/template/schema_test.go | 7 ++----- libs/template/validators.go | 6 +++--- libs/template/validators_test.go | 17 +++++++++-------- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/libs/template/schema_test.go b/libs/template/schema_test.go index fd9395bf..f3b69a0e 100644 --- a/libs/template/schema_test.go +++ b/libs/template/schema_test.go @@ -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) diff --git a/libs/template/validators.go b/libs/template/validators.go index c67c6edd..96995560 100644 --- a/libs/template/validators.go +++ b/libs/template/validators.go @@ -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, } diff --git a/libs/template/validators_test.go b/libs/template/validators_test.go index 5621ef92..3573592f 100644 --- a/libs/template/validators_test.go +++ b/libs/template/validators_test.go @@ -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\"") }