Inconsistent Validation in IntType Between parseValue and parseLiteral #1725
-
Hello, I have a question regarding the input validation logic in IntType (src/Type/Definition/IntType.php). Why is the validation logic different between the parseValue and parseLiteral methods? Is this intentional, and if so, could you please explain the reason behind it? Thank you for your help! public function parseValue($value): int
{
$isInt = is_int($value)
|| (is_float($value) && floor($value) === $value);
if (! $isInt) {
$notInt = Utils::printSafeJson($value);
throw new Error("Int cannot represent non-integer value: {$notInt}");
}
if ($value > self::MAX_INT || $value < self::MIN_INT) {
$outOfRangeInt = Utils::printSafeJson($value);
throw new Error("Int cannot represent non 32-bit signed integer value: {$outOfRangeInt}");
}
return (int) $value;
}
public function parseLiteral(Node $valueNode, ?array $variables = null): int
{
if ($valueNode instanceof IntValueNode) {
$val = (int) $valueNode->value;
if ($valueNode->value === (string) $val && $val >= self::MIN_INT && $val <= self::MAX_INT) {
return $val;
}
}
$notInt = Printer::doPrint($valueNode);
throw new Error("Int cannot represent non-integer value: {$notInt}", $valueNode);
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Beta Was this translation helpful? Give feedback.
parseValue
is used for variable values, which may be provided through a variety of formats. The usual transport layer for GraphQL is JSON over HTTP. JSON comes from JavaScript, so it does only have a single numeric type. Thus, we are more lenient.parseLiteral
deals with values that are provided as part of the query string. The GraphQL language itself does distinguish between Int and Float types, so we can be more strict there.