Skip to content

Commit

Permalink
Separate between base and extended message schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad committed May 1, 2024
1 parent 08bd24c commit e98d244
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ export { DomainEventEmitter } from './lib/events/DomainEventEmitter'
export { EventRegistry } from './lib/events/EventRegistry'
export { FakeListener } from './lib/events/fakes/FakeListener'
export * from './lib/events/eventTypes'
export * from './lib/messages/baseMessageSchemas'
2 changes: 1 addition & 1 deletion packages/core/lib/events/DomainEventEmitter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ const createdEventPayload: CommonEventDefinitionSchemaType<typeof TestEvents.cre
type: 'entity.created',
id: randomUUID(),
metadata: {
timestamp: new Date().toISOString(),
originApp: 'de',
producerApp: 'dede',
schemaVersion: '1',
correlationId: randomUUID(),
},
timestamp: new Date().toISOString(),
}

const updatedEventPayload: CommonEventDefinitionSchemaType<typeof TestEvents.updated> = {
Expand Down
7 changes: 5 additions & 2 deletions packages/core/lib/messages/baseMessageSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import z from 'zod'
export const BASE_MESSAGE_SCHEMA = z.object({
id: z.string().describe('event unique identifier'),
type: z.literal<string>('<replace.me>').describe('event type name'),
timestamp: z.string().datetime().describe('iso 8601 datetime'),
payload: z.optional(z.object({})).describe('event payload based on type'),
})

export const EXTENDED_MESSAGE_SCHEMA = BASE_MESSAGE_SCHEMA.extend({
metadata: z
.object({
schemaVersion: z.string().min(1).describe('base event schema version'),
timestamp: z.string().datetime().describe('iso 8601 datetime'),
producerApp: z.string().min(1).describe('app/service that produced the event'),
originApp: z.string().min(1).describe('app/service that initiated the workflow'),
correlationId: z
Expand All @@ -19,4 +22,4 @@ export const BASE_MESSAGE_SCHEMA = z.object({

export type BaseMessageType = z.infer<typeof BASE_MESSAGE_SCHEMA>

export type MessageMetadata = 'id' | 'timestamp' | 'type' | 'metadata'
export type MessageMetadata = 'id' | 'type' | 'metadata'
6 changes: 3 additions & 3 deletions packages/core/test/testContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { z } from 'zod'

import { DomainEventEmitter } from '../lib/events/DomainEventEmitter'
import { EventRegistry } from '../lib/events/EventRegistry'
import { BASE_MESSAGE_SCHEMA } from '../lib/messages/baseMessageSchemas'
import { EXTENDED_MESSAGE_SCHEMA } from '../lib/messages/baseMessageSchemas'
import type { TransactionObservabilityManager } from '../lib/types/MessageQueueTypes'

export const SINGLETON_CONFIG = { lifetime: Lifetime.SINGLETON }
Expand All @@ -19,7 +19,7 @@ const TestLogger: Logger = pino()

export const TestEvents = {
created: {
schema: BASE_MESSAGE_SCHEMA.extend({
schema: EXTENDED_MESSAGE_SCHEMA.extend({
type: z.literal('entity.created'),
payload: z.object({
message: z.string(),
Expand All @@ -28,7 +28,7 @@ export const TestEvents = {
},

updated: {
schema: BASE_MESSAGE_SCHEMA.extend({
schema: EXTENDED_MESSAGE_SCHEMA.extend({
type: z.literal('entity.updated'),
payload: z.object({
message: z.string(),
Expand Down

0 comments on commit e98d244

Please sign in to comment.