Skip to content

Commit

Permalink
Allow null or undefined inputs on schema level
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloSzx committed Jul 21, 2023
1 parent 1ba7edb commit ca6952f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-cups-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@soundxyz/redis-pubsub": patch
---

Allow null or undefined inputs on schema level
15 changes: 9 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ export function RedisPubSub({
);
}

type Maybe<T> = T | null | undefined;

function createChannel<PublishInput, ChannelData, SubscriberData>({
name,
isLazy = true,
Expand All @@ -282,12 +284,12 @@ export function RedisPubSub({
isLazy?: boolean;
} & (
| {
inputSchema: ZodSchema<ChannelData, ZodTypeDef, PublishInput>;
outputSchema: ZodSchema<SubscriberData, ZodTypeDef, ChannelData>;
inputSchema: ZodSchema<Maybe<ChannelData>, ZodTypeDef, PublishInput>;
outputSchema: ZodSchema<SubscriberData, ZodTypeDef, NonNullable<ChannelData>>;
schema?: never;
}
| {
schema: ZodSchema<SubscriberData, ZodTypeDef, PublishInput>;
schema: ZodSchema<NonNullable<SubscriberData>, ZodTypeDef, Maybe<PublishInput>>;
inputSchema?: never;
outputSchema?: never;
}
Expand Down Expand Up @@ -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<PublishInput>; identifier?: string | number },
...{ value: Maybe<PublishInput>; identifier?: string | number }[],
]
) {
await Promise.all(
Expand All @@ -506,10 +508,11 @@ export function RedisPubSub({

const tracing = enabledLogEvents?.PUBLISH_MESSAGE_EXECUTION_TIME ? getTracing() : null;

let parsedValue: ChannelData | SubscriberData;
let parsedValue: Maybe<ChannelData | SubscriberData>;

try {
parsedValue = await inputSchema.parseAsync(value);
if (parsedValue == null) return;
} catch (err) {
onParseError(err);
return;
Expand Down

0 comments on commit ca6952f

Please sign in to comment.