much 2 yearn
much 2 yearn12mo ago

Use zod schemas for argument validation

I am using the zod helper library to use zod as validation but it I get an error when trying to use the zod schema from another file, but it works when writing the schema in the zMutation
export const createTemplate = zMutation({
args: createTemplateSchema, //<-breaks
// {
// authorId: zid("users"),
// title: z.string(),
// goal: z.union([
// z.literal("bodybuilding"),
// z.literal("powerlifting"),
// z.literal("powerbuilding"),
// z.literal("mobility"),
// z.literal("sport"),
// z.literal("general"),
// ]),
// format: z.union([z.literal("split"), z.literal("program")]),
// weekLength: z.number(),
// days: z.array(DaySchema),
// maxWeeks: z.number(),
// }
export const createTemplate = zMutation({
args: createTemplateSchema, //<-breaks
// {
// authorId: zid("users"),
// title: z.string(),
// goal: z.union([
// z.literal("bodybuilding"),
// z.literal("powerlifting"),
// z.literal("powerbuilding"),
// z.literal("mobility"),
// z.literal("sport"),
// z.literal("general"),
// ]),
// format: z.union([z.literal("split"), z.literal("program")]),
// weekLength: z.number(),
// days: z.array(DaySchema),
// maxWeeks: z.number(),
// }
error: Index signature for type 'string' is missing in type 'ZodObject. Is it possible to pass a defined schema or must all zod validation be done at that scope
3 Replies
lee
lee12mo ago
How is createTemplateSchema defined? From the error message, it sounds like it's a z.object when it should be a plain object
much 2 yearn
much 2 yearnOP12mo ago
// FORM TEMPLATE SCHEMAS
export const createTemplateSchema = z.object({
title: z.string().max(25, { message: "Max 25 characters" }).optional(),
goal: z.union([
z.literal("bodybuilding"),
z.literal("powerlifting"),
z.literal("powerbuilding"),
z.literal("mobility"),
z.literal("sport"),
z.literal("general"),
]),
format: z.union([z.literal("split"), z.literal("program")]),
weekLength: z.number().min(1).max(12),
maxWeeks: z.number(),
weeks: z.array(DaySchema),
});
// FORM TEMPLATE SCHEMAS
export const createTemplateSchema = z.object({
title: z.string().max(25, { message: "Max 25 characters" }).optional(),
goal: z.union([
z.literal("bodybuilding"),
z.literal("powerlifting"),
z.literal("powerbuilding"),
z.literal("mobility"),
z.literal("sport"),
z.literal("general"),
]),
format: z.union([z.literal("split"), z.literal("program")]),
weekLength: z.number().min(1).max(12),
maxWeeks: z.number(),
weeks: z.array(DaySchema),
});
you are right it is a z object that i use for form validation on the front end. What is the best way to reuse this schema to work with convex ? thanks for the reply btw! @lee would I have to store the initial schema as a regular object with zod properties then also export a z.object of the same schema? wondering if there is a better way to just just export one zod schema and type
lee
lee12mo ago
You could try doing { args: createTemplateSchema.shape }. If that doesn't work, i would export both the plain object and the z.object

Did you find this page helpful?