Skip to content

Commit

Permalink
remove rule
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Dec 16, 2021
1 parent b89d37a commit a5d64c7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 23 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ You can find and compare releases at the [GitHub release page](https://github.co
- Rename `ServerConfig` option `persistentQueryLoader` to `persistedQueryLoader`
- Call previously unused methods `EnumType::parseValue()` and `EnumType::parseLiteral()`
- Strongly type `PromiseAdapter::createRejected()` to require `\Throwable`
- Store rules exclusively by class name in `DocumentValidator`
- Move members specific to `NamedType` out of `Type`: `$name`, `$description`, `$config`, `isBuiltInType()`, `assertValid()`

### Added
Expand All @@ -40,6 +39,7 @@ You can find and compare releases at the [GitHub release page](https://github.co
- Allow lazy input object fields
- Add validation rule `UniqueEnumValueNames`
- Add SDL validation rule `UniqueOperationTypes` (#995)
- Add ability to remove custom validation rules after adding them via `DocumentValidator::removeRule()`

### Optimized

Expand Down
15 changes: 12 additions & 3 deletions src/Validator/DocumentValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@

use function array_merge;
use function count;
use function get_class;

/**
* Implements the "Validation" section of the spec.
Expand All @@ -70,7 +69,7 @@
*/
class DocumentValidator
{
/** @var array<class-string<ValidationRule>, ValidationRule> */
/** @var array<string, ValidationRule> */
private static array $rules = [];

/** @var array<class-string<ValidationRule>, ValidationRule> */
Expand Down Expand Up @@ -246,7 +245,17 @@ public static function getRule(string $name): ?ValidationRule
*/
public static function addRule(ValidationRule $rule): void
{
self::$rules[get_class($rule)] = $rule;
self::$rules[$rule->getName()] = $rule;
}

/**
* Remove rule from list of global validation rules.
*
* @api
*/
public static function removeRule(ValidationRule $rule): void
{
unset(self::$rules[$rule->getName()]);
}

/**
Expand Down
58 changes: 52 additions & 6 deletions tests/Validator/CustomRuleTest.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
<?php

declare(strict_types=1);

namespace GraphQL\Tests\Validator;

use GraphQL\Error\Error;
use GraphQL\Language\SourceLocation;
use GraphQL\Tests\ErrorHelper;
use GraphQL\Tests\Validator\CustomRuleTest\CustomExecutableDefinitions;
use GraphQL\Validator\DocumentValidator;
use GraphQL\Validator\Rules\CustomValidationRule;
use GraphQL\Validator\Rules\ExecutableDefinitions;
use GraphQL\Validator\ValidationContext;

class CustomRuleTest extends ValidatorTestCase
final class CustomRuleTest extends ValidatorTestCase
{
public function testAddCustomRule(): void
private const CUSTOM_VALIDATION_RULE_ERROR = 'This is the error we are looking for!';

public function testAddRuleCanReplaceDefaultRules(): void
{
DocumentValidator::addRule(new CustomExecutableDefinitions);
DocumentValidator::addRule(new class extends ExecutableDefinitions
{
public function getName(): string
{
return ExecutableDefinitions::class;
}

public static function nonExecutableDefinitionMessage(string $defName): string
{
return "Custom message including {$defName}";
}
});

$this->expectInvalid(
self::getTestSchema(),
Expand All @@ -29,10 +47,38 @@ public function testAddCustomRule(): void
',
[
ErrorHelper::create(
CustomExecutableDefinitions::nonExecutableDefinitionMessage('Cow'),
'Custom message including Cow',
[new SourceLocation(8, 12)]
)
),
]
);
}

public function testAddRuleUsingCustomValidationRule(): void
{
$customRule = new CustomValidationRule('MyRule', static function (ValidationContext $context): array {
$context->reportError(new Error(self::CUSTOM_VALIDATION_RULE_ERROR));

return [];
});

DocumentValidator::addRule($customRule);

$this->expectInvalid(
self::getTestSchema(),
null,
'
query Foo {
dog {
name
}
}
',
[
ErrorHelper::create(self::CUSTOM_VALIDATION_RULE_ERROR),
]
);

DocumentValidator::removeRule($customRule);
}
}
13 changes: 0 additions & 13 deletions tests/Validator/CustomRuleTest/CustomExecutableDefinitions.php

This file was deleted.

0 comments on commit a5d64c7

Please sign in to comment.