From e98d2440793f6818e17d47c205e0af123cefd0ea Mon Sep 17 00:00:00 2001 From: Igor Savin Date: Wed, 1 May 2024 16:06:06 +0300 Subject: [PATCH] Separate between base and extended message schemas --- packages/core/index.ts | 1 + packages/core/lib/events/DomainEventEmitter.spec.ts | 2 +- packages/core/lib/messages/baseMessageSchemas.ts | 7 +++++-- packages/core/test/testContext.ts | 6 +++--- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/core/index.ts b/packages/core/index.ts index 35bbcf87..18f370d0 100644 --- a/packages/core/index.ts +++ b/packages/core/index.ts @@ -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' diff --git a/packages/core/lib/events/DomainEventEmitter.spec.ts b/packages/core/lib/events/DomainEventEmitter.spec.ts index 7f1ca626..c1db2c2a 100644 --- a/packages/core/lib/events/DomainEventEmitter.spec.ts +++ b/packages/core/lib/events/DomainEventEmitter.spec.ts @@ -17,12 +17,12 @@ const createdEventPayload: CommonEventDefinitionSchemaType = { diff --git a/packages/core/lib/messages/baseMessageSchemas.ts b/packages/core/lib/messages/baseMessageSchemas.ts index a038e61e..d44407b0 100644 --- a/packages/core/lib/messages/baseMessageSchemas.ts +++ b/packages/core/lib/messages/baseMessageSchemas.ts @@ -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('').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 @@ -19,4 +22,4 @@ export const BASE_MESSAGE_SCHEMA = z.object({ export type BaseMessageType = z.infer -export type MessageMetadata = 'id' | 'timestamp' | 'type' | 'metadata' +export type MessageMetadata = 'id' | 'type' | 'metadata' diff --git a/packages/core/test/testContext.ts b/packages/core/test/testContext.ts index 76762d46..0e05d2a3 100644 --- a/packages/core/test/testContext.ts +++ b/packages/core/test/testContext.ts @@ -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 } @@ -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(), @@ -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(),