Mongoose error validating optional schema
I think there's an error when using optional schemas. If the data isn't submitted for those schemas, the Zod validator passes them correctly, but Mongoose gives an error when trying to create the data without those schemas:
Example:
export const ChannelConfigSchema = z.object({ name: z.string().min(1), description: z.string().optional(), avatar: z.string(), color: z.string(), messageStart: z.string().min(4), })optional();
export const ChannelWhatsappConfigSchema = z.object({ businessAccountId: z.string().min(1), phoneNumberId: z.string().min(1), wabaId: z.string().min(1), authorizationCode: z.string().min(1), permissions: z.array(z.string()).optional().default([]), });
export const ChannelSchema = z.object({ _id: zId().optional().default(() => new Types.ObjectId()), alias: z.string().min(1), description: z.string().optional(), state: ChannelStateSchema.optional().default(CHANNEL_STATE.ENABLED), type: ChannelTypeSchema, isEnabledForm: z.boolean().optional().default(false), agentId: zId(COLLECTIONS.AGENTS), tenantId: zId(COLLECTIONS.SUBSCRIPTIONS), config: ChannelConfigSchema.optional(), whatsappConfig: ChannelWhatsappConfigSchema.optional() });
Mongoose error when inserting data:
ValidationError: config.messageStart: Path config.messageStart is required., config.color: Path config.color is required., config.avatar: Path config.avatar is required., config.name: Path config.name is required.
The error persists even if I change the "optional()" at the end of the schema:
export const ChannelConfigSchema = z.object({ name: z.string().min(1), description: z.string().optional(), avatar: z.string(), color: z.string(), messageStart: z.string().min(4), })optional();