From d8b23af0817d65b3b7ebd864416cfd88a4b7d505 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Thu, 25 Mar 2021 18:44:56 +0400 Subject: [PATCH 1/4] Added ability to override methods of `ValuesOfCorrectType`. --- src/Validator/DocumentValidator.php | 2 +- src/Validator/Rules/ValuesOfCorrectType.php | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Validator/DocumentValidator.php b/src/Validator/DocumentValidator.php index 98a5871c8..901b3916f 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); $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); } } From 1ae2376efb53ef9b8caa1b028243054f41780e23 Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Thu, 25 Mar 2021 21:38:27 +0400 Subject: [PATCH 2/4] Just for case. --- src/Validator/DocumentValidator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Validator/DocumentValidator.php b/src/Validator/DocumentValidator.php index 901b3916f..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 = self::getRule(ValuesOfCorrectType::class); + $validator = self::getRule(ValuesOfCorrectType::class) ?? new ValuesOfCorrectType(); $visitor = $validator->getVisitor($context); Visitor::visit($valueNode, Visitor::visitWithTypeInfo($typeInfo, $visitor)); From 477f85793a6d4bd79c7e13dc82948f4ce080debf Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Thu, 25 Mar 2021 22:03:02 +0400 Subject: [PATCH 3/4] Overwriting test. --- tests/Validator/ValuesOfCorrectTypeTest.php | 39 +++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/Validator/ValuesOfCorrectTypeTest.php b/tests/Validator/ValuesOfCorrectTypeTest.php index 35487dfe8..4d62e3912 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,42 @@ public function testListVariablesWithInvalidItem() : void self::assertTrue($errors[0]->isClientSafe()); } + + /** + * @see it('error messages can be overwriten') + */ + 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()); + } } From 4c7f7dfc53348c1719ca5285cfa2c4a81ffba93d Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Fri, 26 Mar 2021 14:34:11 +0400 Subject: [PATCH 4/4] Removed "it('error messages can be overwriten')" --- tests/Validator/ValuesOfCorrectTypeTest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/Validator/ValuesOfCorrectTypeTest.php b/tests/Validator/ValuesOfCorrectTypeTest.php index 4d62e3912..3337b3c66 100644 --- a/tests/Validator/ValuesOfCorrectTypeTest.php +++ b/tests/Validator/ValuesOfCorrectTypeTest.php @@ -1651,9 +1651,6 @@ public function testListVariablesWithInvalidItem() : void self::assertTrue($errors[0]->isClientSafe()); } - /** - * @see it('error messages can be overwriten') - */ public function testOverwriting() : void { DocumentValidator::addRule(