Skip to content

Commit ce6dc97

Browse files
authored
Adding in primitive value validation. (#515)
1 parent 24abfa5 commit ce6dc97

File tree

2 files changed

+145
-9
lines changed

2 files changed

+145
-9
lines changed

internal/validation/testdata/tests.json

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,125 @@
14571457
"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 ",
14581458
"errors": []
14591459
},
1460+
{
1461+
"name": "Validate: No invalid default String variable values",
1462+
"rule": "DefaultValuesOfCorrectType",
1463+
"schema": 0,
1464+
"query": "\n query Foo($a: String = -\"\") {\n field(a: $a)\n }\n ",
1465+
"errors": [
1466+
{
1467+
"message": "Variable \"$a\" of type \"String\" has invalid default value -\"\".\nExpected type \"String\", found -\"\".",
1468+
"locations": [
1469+
{
1470+
"line": 2,
1471+
"column": 30
1472+
}
1473+
]
1474+
}
1475+
]
1476+
},
1477+
{
1478+
"name": "Validate: No invalid default Int variable values/bad input",
1479+
"rule": "DefaultValuesOfCorrectType",
1480+
"schema": 0,
1481+
"query": "\n query Foo($a: Int = -\"\") {\n field(a: $a)\n }\n ",
1482+
"errors": [
1483+
{
1484+
"message": "Variable \"$a\" of type \"Int\" has invalid default value -\"\".\nExpected type \"Int\", found -\"\".",
1485+
"locations": [
1486+
{
1487+
"line": 2,
1488+
"column": 27
1489+
}
1490+
]
1491+
}
1492+
]
1493+
},
1494+
{
1495+
"name": "Validate: No invalid default Int variable values/value out of range",
1496+
"rule": "DefaultValuesOfCorrectType",
1497+
"schema": 0,
1498+
"query": "\n query Foo($a: Int = -2147483649) {\n field(a: $a)\n }\n ",
1499+
"errors": [
1500+
{
1501+
"message": "Variable \"$a\" of type \"Int\" has invalid default value -2147483649.\nExpected type \"Int\", found -2147483649.",
1502+
"locations": [
1503+
{
1504+
"line": 2,
1505+
"column": 27
1506+
}
1507+
]
1508+
}
1509+
]
1510+
},
1511+
{
1512+
"name": "Validate: No invalid default Float variable values",
1513+
"rule": "DefaultValuesOfCorrectType",
1514+
"schema": 0,
1515+
"query": "\n query Foo($a: Float = -\"\") {\n field(a: $a)\n }\n ",
1516+
"errors": [
1517+
{
1518+
"message": "Variable \"$a\" of type \"Float\" has invalid default value -\"\".\nExpected type \"Float\", found -\"\".",
1519+
"locations": [
1520+
{
1521+
"line": 2,
1522+
"column": 29
1523+
}
1524+
]
1525+
}
1526+
]
1527+
},
1528+
{
1529+
"name": "Validate: No invalid default Float variable values/value out of range",
1530+
"rule": "DefaultValuesOfCorrectType",
1531+
"schema": 0,
1532+
"query": "\n query Foo($a: Float = 1.8e+308) {\n field(a: $a)\n }\n ",
1533+
"errors": [
1534+
{
1535+
"message": "Variable \"$a\" of type \"Float\" has invalid default value 1.8e+308.\nExpected type \"Float\", found 1.8e+308.",
1536+
"locations": [
1537+
{
1538+
"line": 2,
1539+
"column": 29
1540+
}
1541+
]
1542+
}
1543+
]
1544+
},
1545+
{
1546+
"name": "Validate: No invalid default Boolean variable values",
1547+
"rule": "DefaultValuesOfCorrectType",
1548+
"schema": 0,
1549+
"query": "\n query Foo($a: Boolean = \"false\") {\n field(a: $a)\n }\n ",
1550+
"errors": [
1551+
{
1552+
"message": "Variable \"$a\" of type \"Boolean\" has invalid default value \"false\".\nExpected type \"Boolean\", found \"false\".",
1553+
"locations": [
1554+
{
1555+
"line": 2,
1556+
"column": 31
1557+
}
1558+
]
1559+
}
1560+
]
1561+
},
1562+
{
1563+
"name": "Validate: No invalid default ID variable values",
1564+
"rule": "DefaultValuesOfCorrectType",
1565+
"schema": 0,
1566+
"query": "\n query Foo($a: ID = false) {\n field(a: $a)\n }\n ",
1567+
"errors": [
1568+
{
1569+
"message": "Variable \"$a\" of type \"ID\" has invalid default value false.\nExpected type \"ID\", found false.",
1570+
"locations": [
1571+
{
1572+
"line": 2,
1573+
"column": 26
1574+
}
1575+
]
1576+
}
1577+
]
1578+
},
14601579
{
14611580
"name": "Validate: No unused variables/uses all variables deeply in inline fragments",
14621581
"rule": "NoUnusedVariables",

internal/validation/validation.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -856,19 +856,15 @@ func validateBasicLit(v *types.PrimitiveValue, t types.Type) bool {
856856
if v.Type != scanner.Int {
857857
return false
858858
}
859-
f, err := strconv.ParseFloat(v.Text, 64)
860-
if err != nil {
861-
panic(err)
862-
}
863-
return f >= math.MinInt32 && f <= math.MaxInt32
859+
return validateBuiltInScalar(v.Text, "Int")
864860
case "Float":
865-
return v.Type == scanner.Int || v.Type == scanner.Float
861+
return (v.Type == scanner.Int || v.Type == scanner.Float) && validateBuiltInScalar(v.Text, "Float")
866862
case "String":
867-
return v.Type == scanner.String
863+
return v.Type == scanner.String && validateBuiltInScalar(v.Text, "String")
868864
case "Boolean":
869-
return v.Type == scanner.Ident && (v.Text == "true" || v.Text == "false")
865+
return v.Type == scanner.Ident && validateBuiltInScalar(v.Text, "Boolean")
870866
case "ID":
871-
return v.Type == scanner.Int || v.Type == scanner.String
867+
return (v.Type == scanner.Int && validateBuiltInScalar(v.Text, "Int")) || (v.Type == scanner.String && validateBuiltInScalar(v.Text, "String"))
872868
default:
873869
//TODO: Type-check against expected type by Unmarshalling
874870
return true
@@ -889,6 +885,27 @@ func validateBasicLit(v *types.PrimitiveValue, t types.Type) bool {
889885
return false
890886
}
891887

888+
func validateBuiltInScalar(v string, n string) bool {
889+
switch n {
890+
case "Int":
891+
f, err := strconv.ParseFloat(v, 64)
892+
if err != nil {
893+
return false
894+
}
895+
return f >= math.MinInt32 && f <= math.MaxInt32
896+
case "Float":
897+
f, fe := strconv.ParseFloat(v, 64)
898+
return fe == nil && f >= math.SmallestNonzeroFloat64 && f <= math.MaxFloat64
899+
case "String":
900+
vl := len(v)
901+
return vl >= 2 && v[0] == '"' && v[vl-1] == '"'
902+
case "Boolean":
903+
return v == "true" || v == "false"
904+
default:
905+
return false
906+
}
907+
}
908+
892909
func canBeFragment(t types.Type) bool {
893910
switch t.(type) {
894911
case *types.ObjectTypeDefinition, *types.InterfaceTypeDefinition, *types.Union:

0 commit comments

Comments
 (0)