diff --git a/src/Validator/DocumentValidator.php b/src/Validator/DocumentValidator.php index 98a5871c8..7cb3e4c7a 100644 --- a/src/Validator/DocumentValidator.php +++ b/src/Validator/DocumentValidator.php @@ -300,7 +300,7 @@ public static function isValidLiteralValue(Type $type, $valueNode) $emptyDoc = new DocumentNode(['definitions' => []]); $typeInfo = new TypeInfo($emptySchema, $type); $context = new ValidationContext($emptySchema, $emptyDoc, $typeInfo); - $validator = new ValuesOfCorrectType(); + $validator = self::getRule(ValuesOfCorrectType::class) ?? new ValuesOfCorrectType(); $visitor = $validator->getVisitor($context); Visitor::visit($valueNode, Visitor::visitWithTypeInfo($typeInfo, $visitor)); diff --git a/src/Validator/Rules/ValuesOfCorrectType.php b/src/Validator/Rules/ValuesOfCorrectType.php index 9c48c1253..7f78b7450 100644 --- a/src/Validator/Rules/ValuesOfCorrectType.php +++ b/src/Validator/Rules/ValuesOfCorrectType.php @@ -64,7 +64,7 @@ public function getVisitor(ValidationContext $context) $context->reportError( new Error( - self::getBadValueMessage((string) $type, Printer::doPrint($node), null, $context, $fieldName), + static::getBadValueMessage((string) $type, Printer::doPrint($node), null, $context, $fieldName), $node ) ); @@ -111,7 +111,7 @@ static function ($field) : string { $context->reportError( new Error( - self::requiredFieldMessage($type->name, $fieldName, (string) $fieldType), + static::requiredFieldMessage($type->name, $fieldName, (string) $fieldType), $node ) ); @@ -135,7 +135,7 @@ static function ($field) : string { $context->reportError( new Error( - self::unknownFieldMessage($parentType->name, $node->name->value, $didYouMean), + static::unknownFieldMessage($parentType->name, $node->name->value, $didYouMean), $node ) ); @@ -147,7 +147,7 @@ static function ($field) : string { } elseif (! $type->getValue($node->value)) { $context->reportError( new Error( - self::getBadValueMessage( + static::getBadValueMessage( $type->name, Printer::doPrint($node), $this->enumTypeSuggestion($type, $node), @@ -198,7 +198,7 @@ private function isValidScalar(ValidationContext $context, ValueNode $node, $fie if (! $type instanceof ScalarType) { $context->reportError( new Error( - self::getBadValueMessage( + static::getBadValueMessage( (string) $locationType, Printer::doPrint($node), $this->enumTypeSuggestion($type, $node), @@ -220,7 +220,7 @@ private function isValidScalar(ValidationContext $context, ValueNode $node, $fie // Ensure a reference to the original error is maintained. $context->reportError( new Error( - self::getBadValueMessage( + static::getBadValueMessage( (string) $locationType, Printer::doPrint($node), $error->getMessage(), @@ -279,10 +279,10 @@ private static function getBadValueMessage($typeName, $valueName, $message = nul if ($context) { $arg = $context->getArgument(); if ($arg) { - return self::badArgumentValueMessage($typeName, $valueName, $fieldName, $arg->name, $message); + return static::badArgumentValueMessage($typeName, $valueName, $fieldName, $arg->name, $message); } } - return self::badValueMessage($typeName, $valueName, $message); + return static::badValueMessage($typeName, $valueName, $message); } } diff --git a/tests/Validator/ValuesOfCorrectTypeTest.php b/tests/Validator/ValuesOfCorrectTypeTest.php index 35487dfe8..3337b3c66 100644 --- a/tests/Validator/ValuesOfCorrectTypeTest.php +++ b/tests/Validator/ValuesOfCorrectTypeTest.php @@ -6,6 +6,7 @@ use GraphQL\Error\FormattedError; use GraphQL\Language\SourceLocation; +use GraphQL\Validator\DocumentValidator; use GraphQL\Validator\Rules\ValuesOfCorrectType; class ValuesOfCorrectTypeTest extends ValidatorTestCase @@ -1649,4 +1650,39 @@ public function testListVariablesWithInvalidItem() : void self::assertTrue($errors[0]->isClientSafe()); } + + public function testOverwriting() : void + { + DocumentValidator::addRule( + new class() extends ValuesOfCorrectType + { + public function getName() + { + return (new ValuesOfCorrectType())->getName(); + } + + public static function badArgumentValueMessage($typeName, $valueName, $fieldName, $argName, $message = null) + { + return 'overwritten'; + } + } + ); + + $errors = $this->expectInvalid( + self::getTestSchema(), + null, + ' + { + complicatedArgs { + stringArgField(stringArg: 1) + } + } + ', + [ + $this->badValueWithMessage('overwritten', 4, 43), + ] + ); + + self::assertTrue($errors[0]->isClientSafe()); + } }