diff --git a/lang/en/validation.php b/lang/en/validation.php index c202602b..5790f723 100644 --- a/lang/en/validation.php +++ b/lang/en/validation.php @@ -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.', ]; diff --git a/src/GraphQL/Schemas/Primary/Substrate/Queries/GetTokensQuery.php b/src/GraphQL/Schemas/Primary/Substrate/Queries/GetTokensQuery.php index 79cd0021..ecd6a72c 100644 --- a/src/GraphQL/Schemas/Primary/Substrate/Queries/GetTokensQuery.php +++ b/src/GraphQL/Schemas/Primary/Substrate/Queries/GetTokensQuery.php @@ -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; @@ -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(); + } + + }, + ], + ]; + } } diff --git a/tests/Feature/GraphQL/Queries/GetTokensTest.php b/tests/Feature/GraphQL/Queries/GetTokensTest.php index fdb212a0..06145f07 100644 --- a/tests/Feature/GraphQL/Queries/GetTokensTest.php +++ b/tests/Feature/GraphQL/Queries/GetTokensTest.php @@ -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 = '';