CustomValidationRule.php
2 months ago
DisableIntrospection.php
2 months ago
ExecutableDefinitions.php
2 months ago
FieldsOnCorrectType.php
2 months ago
FragmentsOnCompositeTypes.php
2 months ago
KnownArgumentNames.php
2 months ago
KnownArgumentNamesOnDirectives.php
2 months ago
KnownDirectives.php
2 months ago
KnownFragmentNames.php
2 months ago
KnownTypeNames.php
2 months ago
LoneAnonymousOperation.php
2 months ago
LoneSchemaDefinition.php
2 months ago
NoFragmentCycles.php
2 months ago
NoUndefinedVariables.php
2 months ago
NoUnusedFragments.php
2 months ago
NoUnusedVariables.php
2 months ago
OneOfInputObjectsRule.php
2 months ago
OverlappingFieldsCanBeMerged.php
2 months ago
PossibleFragmentSpreads.php
2 months ago
PossibleTypeExtensions.php
2 months ago
ProvidedRequiredArguments.php
2 months ago
ProvidedRequiredArgumentsOnDirectives.php
2 months ago
QueryComplexity.php
2 months ago
QueryDepth.php
2 months ago
QuerySecurityRule.php
2 months ago
ScalarLeafs.php
2 months ago
SingleFieldSubscription.php
2 months ago
UniqueArgumentDefinitionNames.php
2 months ago
UniqueArgumentNames.php
2 months ago
UniqueDirectiveNames.php
2 months ago
UniqueDirectivesPerLocation.php
2 months ago
UniqueEnumValueNames.php
2 months ago
UniqueFieldDefinitionNames.php
2 months ago
UniqueFragmentNames.php
2 months ago
UniqueInputFieldNames.php
2 months ago
UniqueOperationNames.php
2 months ago
UniqueOperationTypes.php
2 months ago
UniqueTypeNames.php
2 months ago
UniqueVariableNames.php
2 months ago
ValidationRule.php
2 months ago
ValuesOfCorrectType.php
2 months ago
VariablesAreInputTypes.php
2 months ago
VariablesInAllowedPosition.php
2 months ago
OneOfInputObjectsRule.php
95 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules; |
| 4 | |
| 5 | use Automattic\WooCommerce\Vendor\GraphQL\Error\Error; |
| 6 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\NodeKind; |
| 7 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\ObjectValueNode; |
| 8 | use Automattic\WooCommerce\Vendor\GraphQL\Type\Definition\InputObjectType; |
| 9 | use Automattic\WooCommerce\Vendor\GraphQL\Type\Definition\Type; |
| 10 | use Automattic\WooCommerce\Vendor\GraphQL\Validator\QueryValidationContext; |
| 11 | |
| 12 | /** |
| 13 | * OneOf Input Objects validation rule. |
| 14 | * |
| 15 | * Validates that OneOf Input Objects have exactly one non-null field provided. |
| 16 | */ |
| 17 | class OneOfInputObjectsRule extends ValidationRule |
| 18 | { |
| 19 | public function getVisitor(QueryValidationContext $context): array |
| 20 | { |
| 21 | return [ |
| 22 | NodeKind::OBJECT => static function (ObjectValueNode $node) use ($context): void { |
| 23 | $type = $context->getInputType(); |
| 24 | |
| 25 | if ($type === null) { |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | $namedType = Type::getNamedType($type); |
| 30 | if (! ($namedType instanceof InputObjectType) |
| 31 | || ! $namedType->isOneOf() |
| 32 | ) { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | $providedFields = []; |
| 37 | $nullFields = []; |
| 38 | |
| 39 | foreach ($node->fields as $fieldNode) { |
| 40 | $fieldName = $fieldNode->name->value; |
| 41 | $providedFields[] = $fieldName; |
| 42 | |
| 43 | // Check if the field value is explicitly null |
| 44 | if ($fieldNode->value->kind === NodeKind::NULL) { |
| 45 | $nullFields[] = $fieldName; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | $fieldCount = count($providedFields); |
| 50 | |
| 51 | if ($fieldCount === 0) { |
| 52 | $context->reportError(new Error( |
| 53 | static::oneOfInputObjectExpectedExactlyOneFieldMessage($namedType->name), |
| 54 | [$node] |
| 55 | )); |
| 56 | |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | if ($fieldCount > 1) { |
| 61 | $context->reportError(new Error( |
| 62 | static::oneOfInputObjectExpectedExactlyOneFieldMessage($namedType->name, $fieldCount), |
| 63 | [$node] |
| 64 | )); |
| 65 | |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | // At this point, $fieldCount === 1 |
| 70 | if (count($nullFields) > 0) { |
| 71 | // Exactly one field provided, but it's null |
| 72 | $context->reportError(new Error( |
| 73 | static::oneOfInputObjectFieldValueMustNotBeNullMessage($namedType->name, $nullFields[0]), |
| 74 | [$node] |
| 75 | )); |
| 76 | } |
| 77 | }, |
| 78 | ]; |
| 79 | } |
| 80 | |
| 81 | public static function oneOfInputObjectExpectedExactlyOneFieldMessage(string $typeName, ?int $providedCount = null): string |
| 82 | { |
| 83 | if ($providedCount === null) { |
| 84 | return "OneOf input object '{$typeName}' must specify exactly one field."; |
| 85 | } |
| 86 | |
| 87 | return "OneOf input object '{$typeName}' must specify exactly one field, but {$providedCount} fields were provided."; |
| 88 | } |
| 89 | |
| 90 | public static function oneOfInputObjectFieldValueMustNotBeNullMessage(string $typeName, string $fieldName): string |
| 91 | { |
| 92 | return "OneOf input object '{$typeName}' field '{$fieldName}' must be non-null."; |
| 93 | } |
| 94 | } |
| 95 |