Skip to main content
Version: 0.13.0 (Previous)

✅ Chat validators

Chat routes compose local guards with shared authentication, ownership, and organization validators. Every consumer below links to the authoritative route definition, where the complete invocation and execution order are visible.

Inventory

ValidatorReadsSuccess conditionFailure / errorConsumers
getSubscriptionByChannelAndSubscriber(...)db.subscriptions, channelId, and subscribedIdResolves a matching subscription document or null in a ResultAsync.Preserves the unknown database rejection in Err.hasSubscription(path, subscriberPath?).
channelExists(path)db.chatChannels and a configured payload pathA channel with the resolved ID exists.500 for a missing collection or query failure; 404 when absent.getChannelMessagesRoute, upsertChatChannelReadStateRoute.
hasSubscription(path, subscriberPath?)Configured token extractor, db.subscriptions, channel path, and optional subscriber pathThe resolved (channelId, subscribedId) subscription exists.401 invalid token; 400 missing ID; 403 absent subscription; 500 missing store or query failure.getChatChannelRoute, getChannelMessagesRoute, createChatMessageRoute, findChatMessagesRoute, upsertChatChannelReadStateRoute, findChatSubscriptionsRoute.
hasOrganizationAccessToMessageTemplate(allowedRoles, path)Token, template and organization stores, allowed roles, and template-ID pathThe template belongs to an organization where the identity has an allowed role.401 invalid token; 403 global-only template or disallowed member; 404 absent template/organization; 500 missing store.getChatMessageTemplateRoute, updateChatMessageTemplateRoute, deleteChatMessageTemplateRoute.
isAuthenticated()Configured Bearer or cookie token extractorThe access token is valid.Shared authentication errors, normally 401.createChatChannelRoute, findChatChannelsRoute, getChatChannelRoute, updateChatChannelRoute, deleteChatChannelRoute, getChannelMessagesRoute, getChatChannelIconUploadUrlRoute, createChatMessageTemplateRoute, getChatMessageTemplateRoute, updateChatMessageTemplateRoute, deleteChatMessageTemplateRoute, findChatMessageTemplatesRoute, findChatMessageTemplatesForOrganizationRoute, createChatMessageRoute, findChatMessagesRoute, getChatMessageRoute, updateChatMessageRoute, deleteChatMessageRoute, getChatMessageAttachmentUploadUrlRoute, createChatMessageAttachmentRoute, deleteChatMessageAttachmentRoute, upsertChatChannelReadStateRoute, createChatSubscriptionRoute, findChatSubscriptionsRoute, getChatSubscriptionRoute, deleteChatSubscriptionRoute. The current streamChatMessagesRoute does not compose it.
checkIdentityType(types)Token identity, identity store, and allowed configured type namesThe authenticated identity has an allowed type.Shared authentication/identity lookup and authorization errors.createChatChannelRoute, findChatChannelsRoute, getChatChannelRoute, updateChatChannelRoute, deleteChatChannelRoute, getChatChannelIconUploadUrlRoute, createChatMessageTemplateRoute, getChatMessageTemplateRoute, updateChatMessageTemplateRoute, deleteChatMessageTemplateRoute, findChatMessageTemplatesRoute, findChatMessagesRoute, getChatMessageRoute, deleteChatMessageRoute, deleteChatMessageAttachmentRoute, createChatSubscriptionRoute, findChatSubscriptionsRoute, getChatSubscriptionRoute, deleteChatSubscriptionRoute.
isSelf(path)Token identity and a configured payload pathThe payload value equals the authenticated identity ID.Shared authentication and self-authorization errors.createChatChannelRoute, findChatChannelsRoute, createChatMessageRoute, findChatMessagesRoute, upsertChatChannelReadStateRoute, createChatSubscriptionRoute, findChatSubscriptionsRoute.
ownsChannel(path)Token identity, db.chatChannels, and a channel-ID pathThe channel's ownerId equals the identity ID.Shared resource lookup and ownership errors.getChatChannelRoute, updateChatChannelRoute, deleteChatChannelRoute, getChatChannelIconUploadUrlRoute, findChatSubscriptionsRoute.
ownsMessage(path)Token identity, db.chatMessages, and a message-ID pathThe message's senderId equals the identity ID.Shared resource lookup and ownership errors.getChatMessageRoute, updateChatMessageRoute, deleteChatMessageRoute, getChatMessageAttachmentUploadUrlRoute, createChatMessageAttachmentRoute, deleteChatMessageAttachmentRoute.
ownsSubscription(path)Token identity, db.subscriptions, and a subscription-ID pathThe subscription's subscribedId equals the identity ID.Shared resource lookup and ownership errors.getChatSubscriptionRoute, deleteChatSubscriptionRoute.
hasOrgRole(roles, path)Token identity, organization store, configured role IDs, and organization-ID pathThe identity holds one of the allowed organization roles.Shared organization lookup and authorization errors.createChatMessageTemplateRoute, findChatMessageTemplatesForOrganizationRoute.
some(...)Results or errors from its child validatorsAt least one child authorization branch succeeds.Re-throws after every child branch fails.createChatChannelRoute, findChatChannelsRoute, getChatChannelRoute, updateChatChannelRoute, deleteChatChannelRoute, getChatChannelIconUploadUrlRoute, createChatMessageTemplateRoute, getChatMessageTemplateRoute, updateChatMessageTemplateRoute, deleteChatMessageTemplateRoute, findChatMessagesRoute, getChatMessageRoute, deleteChatMessageRoute, deleteChatMessageAttachmentRoute, createChatSubscriptionRoute, findChatSubscriptionsRoute, getChatSubscriptionRoute, deleteChatSubscriptionRoute.

Details

getSubscriptionByChannelAndSubscriber(...)

Implementation

This helper accepts a MongoDB subscription collection, channelId, and subscribedId. It queries their string values and returns ResultAsync<WithId<Document> | null, unknown> without converting a database rejection. It is not a route validator itself; hasSubscription(path, subscriberPath?) is its only in-SDK consumer.

channelExists(path)

Implementation

Signature: channelExists(path): Validator. This factory reads a channel ID from its payload path and queries context.db.chatChannels. Both consumers pass ['params', 'requestParams', 'channelId']. It succeeds without adding pipeline data, throws 500 for a missing collection or query failure, and throws 404 when the channel is absent.

Consumers: getChannelMessagesRoute and upsertChatChannelReadStateRoute.

hasSubscription(path, subscriberPath?)

Implementation

Signature: hasSubscription(path, subscriberPath?): Validator. This factory uses context.authenticate or the Bearer extractor, requires context.db.subscriptions, and resolves the subscriber from its optional second path or from the token identity. It throws 500 for a missing collection/query failure, 401 for an invalid token, 400 for a missing channel/subscriber ID, and 403 when no matching subscription exists.

Consumers: The channel path is requestParams.channelId in getChatChannelRoute, getChannelMessagesRoute, and upsertChatChannelReadStateRoute; requestBody.channelId in createChatMessageRoute; and requestQuery.channelId in findChatMessagesRoute and findChatSubscriptionsRoute. All six omit the subscriber path and therefore check the token identity.

hasOrganizationAccessToMessageTemplate(allowedRoles, path)

Implementation

Signature: hasOrganizationAccessToMessageTemplate(allowedRoles, path): Validator. This factory authenticates through context.authenticate or Bearer extraction, loads context.db.chatMessageTemplates, follows the template's organizationId into context.db.organizations, and permits a member whose raw role is in allowedRoles. The get, update, and delete template routes all pass ['owner', 'admin'] and ['params', 'requestParams', 'messageTemplateId'].

It throws 401 for an invalid token, 500 for either missing collection, 404 for a missing template or organization, and 403 when the template has no organization or the identity lacks an allowed membership. Each route wraps it in some(...) with the global-admin branch.

Consumers: getChatMessageTemplateRoute, updateChatMessageTemplateRoute, and deleteChatMessageTemplateRoute.

isAuthenticated()

Implementation

checkIdentityType(types)

Implementation

Signature: checkIdentityType(types): Validator. This shared factory checks the token identity against the configured identity type IDs. Chat always passes ['admin']. It is used directly after authentication only by findChatMessageTemplatesRoute; every other consumer in the inventory uses it as the first branch of some(...). See checkIdentityType for shared failures and configuration behavior.

Consumers: createChatChannelRoute, findChatChannelsRoute, getChatChannelRoute, updateChatChannelRoute, deleteChatChannelRoute, getChatChannelIconUploadUrlRoute, createChatMessageTemplateRoute, getChatMessageTemplateRoute, updateChatMessageTemplateRoute, deleteChatMessageTemplateRoute, findChatMessageTemplatesRoute, findChatMessagesRoute, getChatMessageRoute, deleteChatMessageRoute, deleteChatMessageAttachmentRoute, createChatSubscriptionRoute, findChatSubscriptionsRoute, getChatSubscriptionRoute, and deleteChatSubscriptionRoute.

isSelf(path)

Implementation

Signature: isSelf<T extends string>(identityIdPathInPayload: [T, ...T[]]): Validator. This shared factory compares the token identity ID with a request value. Chat passes requestBody.ownerId for createChatChannelRoute, requestQuery.ownerId for findChatChannelsRoute, requestBody.senderId for createChatMessageRoute, requestQuery.senderId for findChatMessagesRoute, requestBody.identityId for upsertChatChannelReadStateRoute, requestBody.subscribedId for createChatSubscriptionRoute, and requestQuery.subscribedId for findChatSubscriptionsRoute. The message-create and read-state usages are direct; the others are branches of some(...). See the canonical isSelf contract.

Consumers: createChatChannelRoute, findChatChannelsRoute, createChatMessageRoute, findChatMessagesRoute, upsertChatChannelReadStateRoute, createChatSubscriptionRoute, and findChatSubscriptionsRoute.

ownsChannel(path)

Implementation

Signature: ownsChannel(path): Validator. This ownsResource specialization reads context.db.chatChannels, resolves the ID at the supplied path, and compares the channel's ownerId with the token identity. It receives requestParams.channelId for the channel get, update, delete, and icon-upload routes, and requestQuery.channelId for findChatSubscriptionsRoute. Every use is a some(...) branch. See the shared ownsResource contract.

Consumers: getChatChannelRoute, updateChatChannelRoute, deleteChatChannelRoute, getChatChannelIconUploadUrlRoute, and findChatSubscriptionsRoute.

ownsMessage(path)

Implementation

Signature: ownsMessage(path): Validator. This ownsResource specialization reads context.db.chatMessages, resolves requestParams.messageId, and compares the message's senderId with the token identity. It is direct on updateChatMessageRoute, getChatMessageAttachmentUploadUrlRoute, and createChatMessageAttachmentRoute; it is a some(...) branch on message get/delete and attachment delete. See the shared ownsResource contract.

Consumers: getChatMessageRoute, updateChatMessageRoute, deleteChatMessageRoute, getChatMessageAttachmentUploadUrlRoute, createChatMessageAttachmentRoute, and deleteChatMessageAttachmentRoute.

ownsSubscription(path)

Implementation

Signature: ownsSubscription(path): Validator. This ownsResource specialization reads context.db.subscriptions, resolves requestParams.subscriptionId, and compares the subscription's subscribedId with the token identity. See the shared ownsResource contract.

Consumers: getChatSubscriptionRoute and deleteChatSubscriptionRoute.

hasOrgRole(roles, path)

Implementation

Signature: hasOrgRole(roles, path): Validator. This shared factory authenticates the identity and checks membership against configured organization role IDs. createChatMessageTemplateRoute passes ['owner', 'admin'] with requestBody.organizationId as a some(...) branch. findChatMessageTemplatesForOrganizationRoute passes the same roles with requestParams.organizationId directly. See the canonical hasOrgRole contract.

Consumers: createChatMessageTemplateRoute and findChatMessageTemplatesForOrganizationRoute.

some(...)

Implementation

Signature: some(...args: Validator[]): Validator. This shared combinator receives already-created validators and succeeds when any branch succeeds. Chat uses it after isAuthenticated() for admin-or-self, admin-or-owner, admin-or-subscriber, and admin-or-organization-member policies. Branches execute in their route-source order, and the combinator rethrows only after every branch fails. See the canonical some contract.

Consumers: createChatChannelRoute, findChatChannelsRoute, getChatChannelRoute, updateChatChannelRoute, deleteChatChannelRoute, getChatChannelIconUploadUrlRoute, createChatMessageTemplateRoute, getChatMessageTemplateRoute, updateChatMessageTemplateRoute, deleteChatMessageTemplateRoute, findChatMessagesRoute, getChatMessageRoute, deleteChatMessageRoute, deleteChatMessageAttachmentRoute, createChatSubscriptionRoute, findChatSubscriptionsRoute, getChatSubscriptionRoute, and deleteChatSubscriptionRoute.