Skip to content

Commit

Permalink
feat(type): support additional number types
Browse files Browse the repository at this point in the history
At present, only Go float64 types are supported. Validating an integer in an
interface{} of an unsupported type currently displays an "unknown" type error.

This adds support for additional Go numeric types: uint, ints and float32s.
  • Loading branch information
saracen committed Jun 29, 2020
1 parent e7af1ef commit d86650c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 5 additions & 1 deletion keywords_standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ func DataType(data interface{}) string {
switch reflect.TypeOf(data).Kind() {
case reflect.Bool:
return "boolean"
case reflect.Float64:

case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return "integer"
case reflect.Float32, reflect.Float64:
number := reflect.ValueOf(data).Float()
if float64(int(number)) == number {
return "integer"
Expand Down
12 changes: 11 additions & 1 deletion schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,17 @@ func TestDataType(t *testing.T) {
{map[string]interface{}{}, "object"},
{struct{}{}, "object"},
{customObject{}, "object"},
{int8(42), "unknown"},
{uint8(42), "integer"},
{uint16(42), "integer"},
{uint32(42), "integer"},
{uint64(42), "integer"},
{int8(42), "integer"},
{int16(42), "integer"},
{int32(42), "integer"},
{int64(42), "integer"},
{float32(42), "integer"},
{float32(42.0), "integer"},
{float32(42.5), "number"},
// special cases which should pass with type hints
{"true", "boolean"},
{4.0, "number"},
Expand Down

0 comments on commit d86650c

Please sign in to comment.