diff --git a/README.md b/README.md index 5e59768..d703b52 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,7 @@ If the value is valid, return `0`, otherwise `1`. ``` ### The `required` property -You can mark any field as `required`, and if the value is not provided, then an automatic validation will happen for you (thus removing the need for you to weaken your validation callback with `null` types). You can set it to `true`, or you can provide an error array similar to the one returned by your validate callback: +You can mark any field as `required`, and if the value is not provided, then an automatic validation will happen for you (thus removing the need for you to weaken your validation callback with `null` types). You can set it to `true`, or you can provide an error array similar to the one returned by your validate callback. You can also set it to a callable that returns the same bool or error array. ```php //... @@ -142,6 +142,15 @@ You can mark any field as `required`, and if the value is not provided, then an } return 1; } + ], + 'naz' => [ + 'required' => static fn() => !Moderator::loggedIn(), + 'validate' => function(string $naz) { + if(Naz::find($naz)) { + return 0; + } + return 1; + } ] ] ]) diff --git a/src/Type/Definition/ValidatedFieldDefinition.php b/src/Type/Definition/ValidatedFieldDefinition.php index e0c61ae..2d59a5a 100644 --- a/src/Type/Definition/ValidatedFieldDefinition.php +++ b/src/Type/Definition/ValidatedFieldDefinition.php @@ -14,8 +14,7 @@ * typeSetter?: callable, * name?: string, * validName?: string, - * partial?: bool, - * required?: bool|array, + * required?: bool|array|callable(): bool|array, * resultName?: string, * args: array, * resolve?: FieldResolver|null, @@ -208,7 +207,6 @@ protected function _validateInputObject(mixed $arg, mixed $value, array &$res, b protected function _validateInputObjectFields(InputObjectType $type, array $objectConfig, mixed $value, array &$res, bool $isParentList = false): void { $createSubErrors = UserErrorsType::needSuberrors($objectConfig, $isParentList); - $isPartial = $objectConfig['partial'] ?? true; $fields = $type->getFields(); foreach ($fields as $key => $field) { @@ -216,6 +214,9 @@ protected function _validateInputObjectFields(InputObjectType $type, array $obje $isKeyPresent = array_key_exists($key, $value); $isRequired = $config['required'] ?? false; + if(is_callable($isRequired)) { + $isRequired = $isRequired(); + } if($isRequired && !isset($value[$key])) { if ($isRequired === true) { $error = ['error' => [1, "$key is required"]]; @@ -224,7 +225,7 @@ protected function _validateInputObjectFields(InputObjectType $type, array $obje $error = ['error' => $isRequired]; } } - else if (!$isPartial || $isKeyPresent) { + else if ($isKeyPresent) { $error = $this->_validate($config, $value[$key] ?? null); } diff --git a/tests/Type/ValidatedFieldDefinition/NonPartialValidation.php b/tests/Type/ValidatedFieldDefinition/RequiredFieldsValidation.php similarity index 92% rename from tests/Type/ValidatedFieldDefinition/NonPartialValidation.php rename to tests/Type/ValidatedFieldDefinition/RequiredFieldsValidation.php index 3caf18e..e8b5f64 100644 --- a/tests/Type/ValidatedFieldDefinition/NonPartialValidation.php +++ b/tests/Type/ValidatedFieldDefinition/RequiredFieldsValidation.php @@ -8,7 +8,7 @@ use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\ValidatedFieldDefinition; -final class NonPartialValidation extends FieldDefinition +final class RequiredFieldsValidation extends FieldDefinition { /** @var mixed[] */ protected $data = [ @@ -27,11 +27,9 @@ public function testInputObjectValidationOnFieldFail(): void 'type' => Type::boolean(), 'args' => [ 'bookAttributes' => [ - 'partial' => false, 'type' => function () { // lazy load return new InputObjectType([ 'name' => 'BookAttributes', - 'partial' => false, 'fields' => [ 'title' => [ 'type' => Type::string(), @@ -68,7 +66,18 @@ public function testInputObjectValidationOnFieldFail(): void return 0; }, ], + 'naz' => [ + 'type' => Type::string(), + 'description' => 'Provide a naz', + 'required' => static fn () => true, + 'validate' => function (string $naz) { + if (strlen($naz) < 10) { + return [1, 'naz must be more than 10 characters!']; + } + return 0; + } + ], 'author' => [ 'type' => Type::id(), 'description' => 'Provide a valid author id', @@ -115,6 +124,10 @@ public function testInputObjectValidationOnFieldFail(): void code msg } + naz { + code + msg + } } } result @@ -147,6 +160,10 @@ public function testInputObjectValidationOnFieldFail(): void 'code' => 1, 'msg' => 'We have no record of that author', ], + 'naz' => [ + 'code' => 1, + 'msg' => 'naz is required', + ], ], ], 'result' => null,