Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add enum value validation in input object #109

Merged
merged 1 commit into from
Nov 7, 2019
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
5 changes: 5 additions & 0 deletions validator/testdata/vars.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ input InputType {
name: String!
nullName: String
nullEmbedded: Embedded
enum: Enum
}

input Embedded {
name: String!
}

enum Enum {
A
}

scalar Custom
16 changes: 13 additions & 3 deletions validator/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package validator

import (
"reflect"
"strings"

"fmt"

Expand Down Expand Up @@ -117,10 +118,19 @@ func (v *varValidator) validateVarType(typ *ast.Type, val reflect.Value) *gqlerr
switch def.Kind {
case ast.Enum:
kind := val.Type().Kind()
if kind == reflect.Int || kind == reflect.Int32 || kind == reflect.Int64 || kind == reflect.String {
return nil
if kind != reflect.Int && kind != reflect.Int32 && kind != reflect.Int64 && kind != reflect.String {
return gqlerror.ErrorPathf(v.path, "enums must be ints or strings")
}
isValidEnum := false
for _, enumVal := range def.EnumValues {
if strings.EqualFold(val.String(), enumVal.Name) {
isValidEnum = true
}
}
return gqlerror.ErrorPathf(v.path, "enums must be ints or strings")
if !isValidEnum {
return gqlerror.ErrorPathf(v.path, "%s is not a valid %s", val.String(), def.Name)
}
return nil
case ast.Scalar:
kind := val.Type().Kind()
switch typ.NamedType {
Expand Down
22 changes: 22 additions & 0 deletions validator/vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,28 @@ func TestValidateVars(t *testing.T) {
})
require.EqualError(t, gerr, "input: variable.var.foobard unknown field")
})

t.Run("enum input object", func(t *testing.T) {
q := gqlparser.MustLoadQuery(schema, `query foo($var: InputType!) { structArg(i: $var) }`)
_, gerr := validator.VariableValues(schema, q.Operations.ForName(""), map[string]interface{}{
"var": map[string]interface{}{
"name": "foobar",
"enum": "A",
},
})
require.Nil(t, gerr)
})

t.Run("unknown enum value input object", func(t *testing.T) {
q := gqlparser.MustLoadQuery(schema, `query foo($var: InputType!) { structArg(i: $var) }`)
_, gerr := validator.VariableValues(schema, q.Operations.ForName(""), map[string]interface{}{
"var": map[string]interface{}{
"name": "foobar",
"enum": "B",
},
})
require.EqualError(t, gerr, "input: variable.var.enum B is not a valid Enum")
})
})

t.Run("array", func(t *testing.T) {
Expand Down