fix: resolve consumer message schemas to the schema output type#472
Conversation
Consumers receive messages that have already been parsed by the consumer schema, so ConsumerMessageSchema and AllConsumerMessageSchemas should resolve to z.output rather than z.input. For schemas without transforms both types are identical, so this changes nothing. They diverge once a schema uses transforms or preprocess (e.g. a field that tolerantly drops unknown enum values): there z.input degrades to unknown and breaks typing in message handlers, while the handler actually receives the parsed output. Publisher-side types intentionally stay z.input, since publishers pass the raw payload that the schema parses on emit. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Warning Review limit reached
More reviews will be available in 10 minutes and 46 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more credits in the billing tab to continue. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughUpdated consumer message schema utility types to derive message types from schema output rather than input. ChangesConsumer Message Type Output Extraction
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
| * Consumers receive messages already parsed by the consumer schema, hence the output type. | ||
| */ | ||
| export type ConsumerMessageSchema<MessageDefinitionType extends CommonEventDefinition> = z.input< | ||
| export type ConsumerMessageSchema<MessageDefinitionType extends CommonEventDefinition> = z.output< |
There was a problem hiding this comment.
can we add type test for this?
There was a problem hiding this comment.
Yep, on it right now :D
There was a problem hiding this comment.
Tests added, and also added a few more on #473
Summary
ConsumerMessageSchemaandAllConsumerMessageSchemascurrently resolve toz.inputof the consumer schema. However, consumers receive messages that have already been parsed by that schema (parseMessagecallsschema.parse(...)before the handler is invoked), so the correct type isz.output.For schemas without transforms
z.inputandz.outputare identical, so this change is invisible to most consumers. They diverge once a schema usestransform/preprocess— for example a forward-compatible field that drops unknown enum variants instead of failing the whole event:With
z.input, the handler seescontext: unknown(the input of apreprocessis alwaysunknown), even though at runtime it receives the parsedz.outputvalue. With this change the handler is typed with what it actually gets.The same fix is applied to
CommonEventDefinitionConsumerSchemaType, which had the same issue and types the handlersDomainEventEmitterinvokes (EventHandler,SingleEventHandler,AnyEventHandler).Publisher-side types (
PublisherMessageSchema,AllPublisherMessageSchemas,CommonEventDefinitionPublisherSchemaType) intentionally stayz.input, since publishers pass the raw payload thatDomainEventEmitter.emitparses.Note on impact
This is a type-level-only change (no runtime behavior). It may surface new type errors for downstream code that relied on the input type of transforming consumer schemas — those were previously typed incorrectly. A minor version bump is probably appropriate.
Testing
typecheckenabled inpackages/schemas, following the existing core/sns setup):lib/utils/messageTypeUtils.types.spec.ts—ConsumerMessageSchemaresolves transformed fields to their output type,PublisherMessageSchemakeeps the raw input,AllConsumerMessageSchemasresolves the parsed union.lib/events/eventTypes.types.spec.ts— same coverage forCommonEventDefinitionConsumerSchemaType/CommonEventDefinitionPublisherSchemaType, plusSingleEventHandler/AnyEventHandlerreceiving the parsed event.packages/coretest/queues/HandlerContainer.types.spec.ts—MessageHandlerConfigBuilder.addConfiginfers the schema output as the handler message type (the path SNS/SQS consumer handlers go through).packages/schemas:biome check+tsc+vitestall passpackages/core,packages/sns,packages/sqs(including test consumers usingConsumerMessageSchema):tsc --noEmitpassesFollow-up
An integration-level type test on
DomainEventEmitteritself (asserting thaton/onAnyhandlers and theemitreturn type see the parsed event) cannot be added in this PR:packages/coredepends on the published@message-queue-toolkit/schemas(^7.0.0), so such a test only compiles once a version containing this fix is released and the dependency is bumped. The equivalent semantics are pinned ineventTypes.types.spec.tsviaSingleEventHandler/AnyEventHandler(the exact typeson/onAnyuse). Happy to add theDomainEventEmitterspec in a follow-up PR after the release.AI Assistance Tracking
We're running a metric to understand where AI assists our engineering work. Please select exactly one of the options below:
Mark "Yes" if AI helped in any part of this work, for example: generating code, refactoring, debugging support, explaining something, reviewing an idea, or suggesting an approach.
🤖 Generated with Claude Code
Summary by CodeRabbit