Skip to content

Remove 'partial' property, and support callable for 'required' #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
//...
Expand All @@ -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;
}
]
]
])
Expand Down
9 changes: 5 additions & 4 deletions src/Type/Definition/ValidatedFieldDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
* typeSetter?: callable,
* name?: string,
* validName?: string,
* partial?: bool,
* required?: bool|array<int,string>,
* required?: bool|array<int,string>|callable(): bool|array<int|string>,
* resultName?: string,
* args: array<UnnamedArgumentConfig>,
* resolve?: FieldResolver|null,
Expand Down Expand Up @@ -208,14 +207,16 @@ 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) {
$config = $field->config;

$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"]];
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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(),
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -115,6 +124,10 @@ public function testInputObjectValidationOnFieldFail(): void
code
msg
}
naz {
code
msg
}
}
}
result
Expand Down Expand Up @@ -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,
Expand Down
Loading