Skip to content

Instantly share code, notes, and snippets.

@ducva
Created January 8, 2024 13:54
Show Gist options
  • Select an option

  • Save ducva/f0c08b511452ca8941e8dc3f10175773 to your computer and use it in GitHub Desktop.

Select an option

Save ducva/f0c08b511452ca8941e8dc3f10175773 to your computer and use it in GitHub Desktop.
zod final
export const validateChannelConfig = z.discriminatedUnion('type', [
z.object({
type: z.literal(ChannelType.LINE),
config: LineChannelConfigSchema
}),
z.object({
type: z.literal(ChannelType.MAIL),
config: MailChannelConfigSchema
})
])
export const ChannelSchema = z.object({
id: z.string(),
name: z.string(),
description: z.string().optional(),
createdAt: z.string(),
updatedAt: z.string(),
config: ChannelConfigSchema
})
.strict()
.refine(
(value) => {
return validateChannelConfig.safeParse({
type: value.type,
config: value.config
}).success
},
{
message: 'Invalid channel config',
path: ['config']
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment