Skip to content
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
3 changes: 2 additions & 1 deletion packages/schemas/lib/events/eventTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export type CommonEventDefinition = {
tags?: readonly string[] // Free-form tags for the event
}

export type CommonEventDefinitionConsumerSchemaType<T extends CommonEventDefinition> = z.input<
// Consumers receive messages already parsed by the consumer schema, hence the output type.
export type CommonEventDefinitionConsumerSchemaType<T extends CommonEventDefinition> = z.output<
T['consumerSchema']
>

Expand Down
75 changes: 75 additions & 0 deletions packages/schemas/lib/events/eventTypes.types.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { expectTypeOf } from 'vitest'
import { z } from 'zod/v4'
import { enrichMessageSchemaWithBase } from '../messages/baseMessageSchemas.ts'
import type {
AnyEventHandler,
CommonEventDefinition,
CommonEventDefinitionConsumerSchemaType,
CommonEventDefinitionPublisherSchemaType,
SingleEventHandler,
} from './eventTypes.ts'

const myEvents = {
plainEvent: {
...enrichMessageSchemaWithBase('entity.created', z.object({ name: z.string() })),
},
transformingEvent: {
...enrichMessageSchemaWithBase(
'entity.updated',
z.object({
// Forward-compatible field: unknown values are dropped instead of failing validation
mode: z.preprocess(
(value) => (value === 'live' ? value : undefined),
z.literal('live').optional(),
),
}),
),
},
} as const satisfies Record<string, CommonEventDefinition>

describe('eventTypes', () => {
describe('CommonEventDefinitionConsumerSchemaType', () => {
it('resolves transformed fields to their output type, not their input type', () => {
type ConsumerMessage = CommonEventDefinitionConsumerSchemaType<
typeof myEvents.transformingEvent
>

// DomainEventEmitter hands handlers the event parsed by the schema
expectTypeOf<ConsumerMessage['payload']['mode']>().toEqualTypeOf<'live' | undefined>()
})
})

describe('CommonEventDefinitionPublisherSchemaType', () => {
it('keeps the input type for transformed fields', () => {
type PublisherMessage = CommonEventDefinitionPublisherSchemaType<
typeof myEvents.transformingEvent
>

// Publishers pass the raw payload that the schema parses on emit
expectTypeOf<PublisherMessage['payload']['mode']>().toBeUnknown()
})
})

describe('SingleEventHandler', () => {
it('receives the parsed event', () => {
type Handler = SingleEventHandler<[typeof myEvents.transformingEvent], 'entity.updated'>
type HandledEvent = Parameters<Handler['handleEvent']>[0]

expectTypeOf<HandledEvent['payload']['mode']>().toEqualTypeOf<'live' | undefined>()
})
})

describe('AnyEventHandler', () => {
it('receives the parsed event union', () => {
type Handler = AnyEventHandler<
[typeof myEvents.plainEvent, typeof myEvents.transformingEvent]
>
type HandledEvent = Parameters<Handler['handleEvent']>[0]

expectTypeOf<HandledEvent['type']>().toEqualTypeOf<'entity.created' | 'entity.updated'>()
expectTypeOf<
Extract<HandledEvent, { type: 'entity.updated' }>['payload']['mode']
>().toEqualTypeOf<'live' | undefined>()
})
})
})
10 changes: 6 additions & 4 deletions packages/schemas/lib/utils/messageTypeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import type { z } from 'zod/v4'
import type { CommonEventDefinition } from '../events/eventTypes.ts'

/**
* Resolves schema of a consumer message for a given event definition
* Resolves schema of a consumer message for a given event definition.
* 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<

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add type test for this?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, on it right now :D

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests added, and also added a few more on #473

MessageDefinitionType['consumerSchema']
>

Expand All @@ -23,7 +24,8 @@ export type AllPublisherMessageSchemas<MessageDefinitionTypes extends CommonEven
z.input<MessageDefinitionTypes[number]['publisherSchema']>

/**
* Resolves schema of all possible consumer messages for a given list of event definitions
* Resolves schema of all possible consumer messages for a given list of event definitions.
* Consumers receive messages already parsed by the consumer schema, hence the output type.
*/
export type AllConsumerMessageSchemas<MessageDefinitionTypes extends CommonEventDefinition[]> =
z.input<MessageDefinitionTypes[number]['consumerSchema']>
z.output<MessageDefinitionTypes[number]['consumerSchema']>
71 changes: 71 additions & 0 deletions packages/schemas/lib/utils/messageTypeUtils.types.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { expectTypeOf } from 'vitest'
import { z } from 'zod/v4'
import type { CommonEventDefinition } from '../events/eventTypes.ts'
import { enrichMessageSchemaWithBase } from '../messages/baseMessageSchemas.ts'
import type {
AllConsumerMessageSchemas,
ConsumerMessageSchema,
PublisherMessageSchema,
} from './messageTypeUtils.ts'

const myEvents = {
plainEvent: {
...enrichMessageSchemaWithBase('entity.created', z.object({ name: z.string() })),
},
transformingEvent: {
...enrichMessageSchemaWithBase(
'entity.updated',
z.object({
// Forward-compatible field: unknown values are dropped instead of failing validation
mode: z.preprocess(
(value) => (value === 'live' ? value : undefined),
z.literal('live').optional(),
),
}),
),
},
} as const satisfies Record<string, CommonEventDefinition>

describe('messageTypeUtils', () => {
describe('ConsumerMessageSchema', () => {
it('resolves to the parsed message type for transform-free schemas', () => {
type ConsumerMessage = ConsumerMessageSchema<typeof myEvents.plainEvent>

expectTypeOf<ConsumerMessage['type']>().toEqualTypeOf<'entity.created'>()
expectTypeOf<ConsumerMessage['payload']['name']>().toEqualTypeOf<string>()
})

it('resolves transformed fields to their output type, not their input type', () => {
type ConsumerMessage = ConsumerMessageSchema<typeof myEvents.transformingEvent>

// Consumers receive messages already parsed by the consumer schema, so the
// field is the preprocess output, not unknown (the input of any preprocess)
expectTypeOf<ConsumerMessage['payload']['mode']>().toEqualTypeOf<'live' | undefined>()
})
})

describe('PublisherMessageSchema', () => {
it('resolves to the raw (pre-parse) message type', () => {
type PublisherMessage = PublisherMessageSchema<typeof myEvents.plainEvent>

expectTypeOf<PublisherMessage['payload']['name']>().toEqualTypeOf<string>()
})

it('keeps the input type for transformed fields', () => {
type PublisherMessage = PublisherMessageSchema<typeof myEvents.transformingEvent>

// Publishers pass the raw payload that the schema parses on emit
expectTypeOf<PublisherMessage['payload']['mode']>().toBeUnknown()
})
})

describe('AllConsumerMessageSchemas', () => {
it('resolves to the union of parsed message types', () => {
type SupportedMessages = AllConsumerMessageSchemas<
[typeof myEvents.plainEvent, typeof myEvents.transformingEvent]
>

expectTypeOf<SupportedMessages['type']>().toEqualTypeOf<'entity.created' | 'entity.updated'>()
})
})
})
4 changes: 4 additions & 0 deletions packages/schemas/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export default defineConfig({
watch: false,
mockReset: true,
pool: 'threads',
typecheck: {
enabled: true,
include: ['**/*.types.spec.ts'],
},
coverage: {
provider: 'v8',
include: ['lib/**/*.ts'],
Expand Down
Loading