From 5eabeeea209a7ac806d9498e2bb926d9b9e571cd Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Wed, 6 May 2026 12:10:50 +0530 Subject: [PATCH] refactor: update payload validators to use closures for instantiation and improve error handling --- src/Appwrite/Realtime/Message/Dispatcher.php | 10 +------ .../Message/Handlers/SubscribeHandler.php | 2 +- .../Message/Handlers/UnsubscribeHandler.php | 2 +- .../Validators/SubscribePayloadValidator.php | 28 ++++++++++--------- .../UnsubscribePayloadValidator.php | 17 ++++++----- 5 files changed, 26 insertions(+), 33 deletions(-) diff --git a/src/Appwrite/Realtime/Message/Dispatcher.php b/src/Appwrite/Realtime/Message/Dispatcher.php index 47ba4a6cb8..ad766309dd 100644 --- a/src/Appwrite/Realtime/Message/Dispatcher.php +++ b/src/Appwrite/Realtime/Message/Dispatcher.php @@ -117,17 +117,9 @@ class Dispatcher $validator = $validator(); } if (!$validator->isValid($value)) { - // taking the error directly instead of the message attribute from the validator - // to avoid the race condition between coroutines modifying each others static attributes - // as the validator is a static class - $description = $validator->getDescription(); - if (\is_callable([$validator, 'getValidationError'])) { - $description = $validator->getValidationError($value) ?? $description; - } - throw new Exception( Exception::REALTIME_MESSAGE_FORMAT_INVALID, - \sprintf('%s: %s', $key, $description) + \sprintf('%s: %s', $key, $validator->getDescription()) ); } } diff --git a/src/Appwrite/Realtime/Message/Handlers/SubscribeHandler.php b/src/Appwrite/Realtime/Message/Handlers/SubscribeHandler.php index 0e95d869a6..5de891d6c0 100644 --- a/src/Appwrite/Realtime/Message/Handlers/SubscribeHandler.php +++ b/src/Appwrite/Realtime/Message/Handlers/SubscribeHandler.php @@ -21,7 +21,7 @@ class SubscribeHandler extends Action ->desc('Bulk subscribe to realtime channels') ->label(Dispatcher::LABEL_MESSAGE_TYPE, 'subscribe') ->label(Dispatcher::LABEL_PAYLOAD_SHAPE, Dispatcher::PAYLOAD_SHAPE_LIST) - ->param('items', null, new SubscribePayloadValidator(), 'Subscriptions to add') + ->param('items', null, fn () => new SubscribePayloadValidator(), 'Subscriptions to add') ->inject('connection') ->inject('realtime') ->inject('register') diff --git a/src/Appwrite/Realtime/Message/Handlers/UnsubscribeHandler.php b/src/Appwrite/Realtime/Message/Handlers/UnsubscribeHandler.php index a6e6a38179..4b19f99d4b 100644 --- a/src/Appwrite/Realtime/Message/Handlers/UnsubscribeHandler.php +++ b/src/Appwrite/Realtime/Message/Handlers/UnsubscribeHandler.php @@ -17,7 +17,7 @@ class UnsubscribeHandler extends Action ->desc('Bulk remove subscriptions by id') ->label(Dispatcher::LABEL_MESSAGE_TYPE, 'unsubscribe') ->label(Dispatcher::LABEL_PAYLOAD_SHAPE, Dispatcher::PAYLOAD_SHAPE_LIST) - ->param('items', null, new UnsubscribePayloadValidator(), 'Subscriptions to remove') + ->param('items', null, fn () => new UnsubscribePayloadValidator(), 'Subscriptions to remove') ->inject('connection') ->inject('realtime') ->inject('register') diff --git a/src/Appwrite/Realtime/Message/Validators/SubscribePayloadValidator.php b/src/Appwrite/Realtime/Message/Validators/SubscribePayloadValidator.php index d3c9a4cc37..763a90f49f 100644 --- a/src/Appwrite/Realtime/Message/Validators/SubscribePayloadValidator.php +++ b/src/Appwrite/Realtime/Message/Validators/SubscribePayloadValidator.php @@ -6,9 +6,11 @@ use Utopia\Validator; class SubscribePayloadValidator extends Validator { + protected string $description = 'Payload is not valid.'; + public function getDescription(): string { - return 'Payload is not valid.'; + return $this->description; } public function isArray(): bool @@ -22,33 +24,33 @@ class SubscribePayloadValidator extends Validator } public function isValid(mixed $value): bool - { - return $this->getValidationError($value) === null; - } - - public function getValidationError(mixed $value): ?string { if (!\is_array($value) || !\array_is_list($value)) { - return 'Payload is not valid.'; + $this->description = 'Payload is not valid.'; + return false; } foreach ($value as $payload) { if (!\is_array($payload)) { - return 'Each subscribe payload must be an object.'; + $this->description = 'Each subscribe payload must be an object.'; + return false; } if (!\array_key_exists('channels', $payload)) { - return 'channels is not present in payload.'; + $this->description = 'channels is not present in payload.'; + return false; } if (!\is_array($payload['channels']) || !\array_is_list($payload['channels'])) { - return 'channels is not a valid array.'; + $this->description = 'channels is not a valid array.'; + return false; } if (\array_key_exists('queries', $payload) && (!\is_array($payload['queries']) || !\array_is_list($payload['queries'])) ) { - return 'queries is not a valid array.'; + $this->description = 'queries is not a valid array.'; + return false; } } - return null; + return true; } -} +} \ No newline at end of file diff --git a/src/Appwrite/Realtime/Message/Validators/UnsubscribePayloadValidator.php b/src/Appwrite/Realtime/Message/Validators/UnsubscribePayloadValidator.php index e3e3742f43..91c26ac0a2 100644 --- a/src/Appwrite/Realtime/Message/Validators/UnsubscribePayloadValidator.php +++ b/src/Appwrite/Realtime/Message/Validators/UnsubscribePayloadValidator.php @@ -6,9 +6,11 @@ use Utopia\Validator; class UnsubscribePayloadValidator extends Validator { + protected string $description = 'Payload is not valid.'; + public function getDescription(): string { - return 'Payload is not valid.'; + return $this->description; } public function isArray(): bool @@ -22,14 +24,10 @@ class UnsubscribePayloadValidator extends Validator } public function isValid(mixed $value): bool - { - return $this->getValidationError($value) === null; - } - - public function getValidationError(mixed $value): ?string { if (!\is_array($value) || !\array_is_list($value)) { - return 'Payload is not valid.'; + $this->description = 'Payload is not valid.'; + return false; } foreach ($value as $payload) { @@ -39,10 +37,11 @@ class UnsubscribePayloadValidator extends Validator || !\is_string($payload['subscriptionId']) || $payload['subscriptionId'] === '' ) { - return 'Each unsubscribe payload must include a non-empty subscriptionId.'; + $this->description = 'Each unsubscribe payload must include a non-empty subscriptionId.'; + return false; } } - return null; + return true; } }