Skip to content
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

[PLA-1946] Add after param validation #268

Merged
merged 1 commit into from
Oct 14, 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
1 change: 1 addition & 0 deletions lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@
'string_too_large' => 'The :attribute field is too large.',
'not_daemon_wallet' => 'The :attribute is a daemon wallet and should not be used as a signingAccount.',
'keep_existential_deposit' => 'The :attribute is not enough to keep the existential deposit of :existentialDeposit.',
'invalid_after' => 'The :attribute contains and invalid encoded data.',
];
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Enjin\Platform\Models\Token;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Illuminate\Pagination\Cursor;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Rebing\GraphQL\Support\Facades\GraphQL;
Expand Down Expand Up @@ -81,4 +82,22 @@ public function resolve($root, $args, $context, ResolveInfo $resolveInfo, Closur
->when(Arr::get($args, 'tokenIds'), fn ($query) => $query->whereIn('token_chain_id', $args['tokenIds']))
->cursorPaginateWithTotalDesc('identifier', $args['first']);
}

/**
* Get the validatio rules.
*/
protected function rules(array $args = []): array
{
return [
'after' => [
'nullable',
function (string $attribute, mixed $value, Closure $fail): void {
if (!Arr::get(Cursor::fromEncoded($value)?->toArray(), 'identifier')) {
$fail('enjin-platform::validation.invalid_after')->translate();
}

},
],
];
}
}
13 changes: 13 additions & 0 deletions tests/Feature/GraphQL/Queries/GetTokensTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ public function test_it_can_fetch_tokens_from_a_collection(): void
$this->assertTrue($response['totalCount'] >= 1);
}

public function test_it_can_validate_invalid_after_param(): void
{
$response = $this->graphql($this->method, [
'collectionId' => $this->collection->collection_chain_id,
'after' => 'eyJjb2xsZWN0aW9uX2lkIjoxMDM2MywiX3BvaW50c1RvTmV4dEl0ZW1zIjp0cnVlfQ',
], true);

$this->assertEquals(
['after' => ['The after contains and invalid encoded data.']],
$response['error'],
);
}

public function test_it_can_fetch_tokens_next_page_from_a_collection(): void
{
$after = '';
Expand Down
Loading