Skip to content

Adding primitive value validation #515

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

Merged
merged 1 commit into from
Apr 13, 2022
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
119 changes: 119 additions & 0 deletions internal/validation/testdata/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,125 @@
"query": "\n query Foo($a: String, $b: String, $c: String) {\n field(a: $a) {\n field(b: $b) {\n field(c: $c)\n }\n }\n }\n ",
"errors": []
},
{
"name": "Validate: No invalid default String variable values",
"rule": "DefaultValuesOfCorrectType",
"schema": 0,
"query": "\n query Foo($a: String = -\"\") {\n field(a: $a)\n }\n ",
"errors": [
{
"message": "Variable \"$a\" of type \"String\" has invalid default value -\"\".\nExpected type \"String\", found -\"\".",
"locations": [
{
"line": 2,
"column": 30
}
]
}
]
},
{
"name": "Validate: No invalid default Int variable values/bad input",
"rule": "DefaultValuesOfCorrectType",
"schema": 0,
"query": "\n query Foo($a: Int = -\"\") {\n field(a: $a)\n }\n ",
"errors": [
{
"message": "Variable \"$a\" of type \"Int\" has invalid default value -\"\".\nExpected type \"Int\", found -\"\".",
"locations": [
{
"line": 2,
"column": 27
}
]
}
]
},
{
"name": "Validate: No invalid default Int variable values/value out of range",
"rule": "DefaultValuesOfCorrectType",
"schema": 0,
"query": "\n query Foo($a: Int = -2147483649) {\n field(a: $a)\n }\n ",
"errors": [
{
"message": "Variable \"$a\" of type \"Int\" has invalid default value -2147483649.\nExpected type \"Int\", found -2147483649.",
"locations": [
{
"line": 2,
"column": 27
}
]
}
]
},
{
"name": "Validate: No invalid default Float variable values",
"rule": "DefaultValuesOfCorrectType",
"schema": 0,
"query": "\n query Foo($a: Float = -\"\") {\n field(a: $a)\n }\n ",
"errors": [
{
"message": "Variable \"$a\" of type \"Float\" has invalid default value -\"\".\nExpected type \"Float\", found -\"\".",
"locations": [
{
"line": 2,
"column": 29
}
]
}
]
},
{
"name": "Validate: No invalid default Float variable values/value out of range",
"rule": "DefaultValuesOfCorrectType",
"schema": 0,
"query": "\n query Foo($a: Float = 1.8e+308) {\n field(a: $a)\n }\n ",
"errors": [
{
"message": "Variable \"$a\" of type \"Float\" has invalid default value 1.8e+308.\nExpected type \"Float\", found 1.8e+308.",
"locations": [
{
"line": 2,
"column": 29
}
]
}
]
},
{
"name": "Validate: No invalid default Boolean variable values",
"rule": "DefaultValuesOfCorrectType",
"schema": 0,
"query": "\n query Foo($a: Boolean = \"false\") {\n field(a: $a)\n }\n ",
"errors": [
{
"message": "Variable \"$a\" of type \"Boolean\" has invalid default value \"false\".\nExpected type \"Boolean\", found \"false\".",
"locations": [
{
"line": 2,
"column": 31
}
]
}
]
},
{
"name": "Validate: No invalid default ID variable values",
"rule": "DefaultValuesOfCorrectType",
"schema": 0,
"query": "\n query Foo($a: ID = false) {\n field(a: $a)\n }\n ",
"errors": [
{
"message": "Variable \"$a\" of type \"ID\" has invalid default value false.\nExpected type \"ID\", found false.",
"locations": [
{
"line": 2,
"column": 26
}
]
}
]
},
{
"name": "Validate: No unused variables/uses all variables deeply in inline fragments",
"rule": "NoUnusedVariables",
Expand Down
35 changes: 26 additions & 9 deletions internal/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,19 +856,15 @@ func validateBasicLit(v *types.PrimitiveValue, t types.Type) bool {
if v.Type != scanner.Int {
return false
}
f, err := strconv.ParseFloat(v.Text, 64)
if err != nil {
panic(err)
}
return f >= math.MinInt32 && f <= math.MaxInt32
return validateBuiltInScalar(v.Text, "Int")
case "Float":
return v.Type == scanner.Int || v.Type == scanner.Float
return (v.Type == scanner.Int || v.Type == scanner.Float) && validateBuiltInScalar(v.Text, "Float")
case "String":
return v.Type == scanner.String
return v.Type == scanner.String && validateBuiltInScalar(v.Text, "String")
case "Boolean":
return v.Type == scanner.Ident && (v.Text == "true" || v.Text == "false")
return v.Type == scanner.Ident && validateBuiltInScalar(v.Text, "Boolean")
case "ID":
return v.Type == scanner.Int || v.Type == scanner.String
return (v.Type == scanner.Int && validateBuiltInScalar(v.Text, "Int")) || (v.Type == scanner.String && validateBuiltInScalar(v.Text, "String"))
default:
//TODO: Type-check against expected type by Unmarshalling
return true
Expand All @@ -889,6 +885,27 @@ func validateBasicLit(v *types.PrimitiveValue, t types.Type) bool {
return false
}

func validateBuiltInScalar(v string, n string) bool {
switch n {
case "Int":
f, err := strconv.ParseFloat(v, 64)
if err != nil {
return false
}
return f >= math.MinInt32 && f <= math.MaxInt32
case "Float":
f, fe := strconv.ParseFloat(v, 64)
return fe == nil && f >= math.SmallestNonzeroFloat64 && f <= math.MaxFloat64
case "String":
vl := len(v)
return vl >= 2 && v[0] == '"' && v[vl-1] == '"'
case "Boolean":
return v == "true" || v == "false"
default:
return false
}
}

func canBeFragment(t types.Type) bool {
switch t.(type) {
case *types.ObjectTypeDefinition, *types.InterfaceTypeDefinition, *types.Union:
Expand Down