Skip to content

Commit

Permalink
Merge 477f857 into 65428ce
Browse files Browse the repository at this point in the history
  • Loading branch information
LastDragon-ru committed Mar 25, 2021
2 parents 65428ce + 477f857 commit 6085a45
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Validator/DocumentValidator.php
Expand Up @@ -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));

Expand Down
16 changes: 8 additions & 8 deletions src/Validator/Rules/ValuesOfCorrectType.php
Expand Up @@ -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
)
);
Expand Down Expand Up @@ -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
)
);
Expand All @@ -137,7 +137,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
)
);
Expand All @@ -149,7 +149,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),
Expand Down Expand Up @@ -200,7 +200,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),
Expand All @@ -222,7 +222,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(),
Expand Down Expand Up @@ -281,10 +281,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);
}
}
39 changes: 39 additions & 0 deletions tests/Validator/ValuesOfCorrectTypeTest.php
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 6085a45

Please sign in to comment.