diff --git a/.changeset/plenty-cups-stare.md b/.changeset/plenty-cups-stare.md new file mode 100644 index 0000000..91d987f --- /dev/null +++ b/.changeset/plenty-cups-stare.md @@ -0,0 +1,5 @@ +--- +"@soundxyz/redis-pubsub": patch +--- + +Allow null or undefined inputs on schema level diff --git a/src/index.ts b/src/index.ts index c84b243..2fa8dc0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -270,6 +270,8 @@ export function RedisPubSub({ ); } + type Maybe = T | null | undefined; + function createChannel({ name, isLazy = true, @@ -282,12 +284,12 @@ export function RedisPubSub({ isLazy?: boolean; } & ( | { - inputSchema: ZodSchema; - outputSchema: ZodSchema; + inputSchema: ZodSchema, ZodTypeDef, PublishInput>; + outputSchema: ZodSchema>; schema?: never; } | { - schema: ZodSchema; + schema: ZodSchema, ZodTypeDef, Maybe>; inputSchema?: never; outputSchema?: never; } @@ -496,8 +498,8 @@ export function RedisPubSub({ async function publish( ...values: [ - { value: PublishInput | null | undefined; identifier?: string | number }, - ...{ value: PublishInput | null | undefined; identifier?: string | number }[], + { value: Maybe; identifier?: string | number }, + ...{ value: Maybe; identifier?: string | number }[], ] ) { await Promise.all( @@ -506,10 +508,11 @@ export function RedisPubSub({ const tracing = enabledLogEvents?.PUBLISH_MESSAGE_EXECUTION_TIME ? getTracing() : null; - let parsedValue: ChannelData | SubscriberData; + let parsedValue: Maybe; try { parsedValue = await inputSchema.parseAsync(value); + if (parsedValue == null) return; } catch (err) { onParseError(err); return;