Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions packages/convex-helpers/server/zod3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,60 @@ test("output validators work for arrays objects and unions", async () => {
expect(union.members[1].isOptional).toBe("required");
});

test("zodOutputToConvex infers required types for fields with defaults", () => {
const ZSimpleObject = z.object({
age: z.number().default(25),
name: z.string(),
status: z.union([z.literal("active"), z.literal("inactive")]).default("active"),
});
const _VSimpleObject = zodOutputToConvex(ZSimpleObject);
type TZodSimpleObject = z.infer<typeof ZSimpleObject>;
type TConvexSimpleObject = Infer<typeof _VSimpleObject>;

// Fields with defaults should be required (not optional) in the output type
expectTypeOf<TConvexSimpleObject["age"]>().toEqualTypeOf<TZodSimpleObject["age"]>();
expectTypeOf<TConvexSimpleObject["status"]>().toEqualTypeOf<
TZodSimpleObject["status"]
>();
expectTypeOf<TConvexSimpleObject>().toEqualTypeOf<TZodSimpleObject>();

// Validator objects should have isOptional: "required" for fields with defaults
expect(_VSimpleObject.fields.age.isOptional).toBe("required");
expect(_VSimpleObject.fields.status.isOptional).toBe("required");
expect(_VSimpleObject.fields.name.isOptional).toBe("required");

// Test nested objects with defaults
const ZNestedObject = z.object({
id: z.string(),
profile: z.object({
displayName: z.string().default("Anonymous"),
isPublic: z.boolean().default(false),
}),
tags: z.array(z.string()).default([]),
});
const _VNestedObject = zodOutputToConvex(ZNestedObject);
type TZodNestedObject = z.infer<typeof ZNestedObject>;
type TConvexNestedObject = Infer<typeof _VNestedObject>;

// Nested object fields with defaults should be required
expectTypeOf<TConvexNestedObject["tags"]>().toEqualTypeOf<
TZodNestedObject["tags"]
>();
expectTypeOf<TConvexNestedObject["profile"]["displayName"]>().toEqualTypeOf<
TZodNestedObject["profile"]["displayName"]
>();
expectTypeOf<TConvexNestedObject["profile"]["isPublic"]>().toEqualTypeOf<
TZodNestedObject["profile"]["isPublic"]
>();
expectTypeOf<TConvexNestedObject>().toEqualTypeOf<TZodNestedObject>();

// Nested validator objects should have isOptional: "required" for fields with defaults
expect(_VNestedObject.fields.tags.isOptional).toBe("required");
expect(_VNestedObject.fields.profile.fields.displayName.isOptional).toBe("required");
expect(_VNestedObject.fields.profile.fields.isPublic.isOptional).toBe("required");
expect(_VNestedObject.fields.id.isOptional).toBe("required");
});

test("zod output compliance", async () => {
const t = convexTest(schema, modules);
const response = await t.query(testApi.zodOutputCompliance, {});
Expand Down
28 changes: 26 additions & 2 deletions packages/convex-helpers/server/zod3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,30 @@ type ConvexObjectValidatorFromZod<T extends ZodValidator> = VObject<
}
>;

type ConvexObjectValidatorFromZodOutput<T extends ZodValidator> = VObject<
ObjectType<{
[key in keyof T]: T[key] extends z.ZodTypeAny
? ConvexValidatorFromZodOutput<T[key]>
: never;
}>,
{
[key in keyof T]: ConvexValidatorFromZodOutput<T[key]>;
}
>;

type ConvexUnionValidatorFromZodOutput<T> = T extends z.ZodTypeAny[]
? VUnion<
ConvexValidatorFromZodOutput<T[number]>["type"],
{
[Index in keyof T]: T[Index] extends z.ZodTypeAny
? ConvexValidatorFromZodOutput<T[Index]>
: never;
},
"required",
ConvexValidatorFromZodOutput<T[number]>["fieldPaths"]
>
: never;

/**
* Converts a Zod validator type
* to the corresponding Convex validator type from `convex/values`.
Expand Down Expand Up @@ -1040,9 +1064,9 @@ export type ConvexValidatorFromZodOutput<Z extends z.ZodTypeAny> =
ConvexValidatorFromZodOutput<Inner>
>
: Z extends z.ZodObject<infer ZodShape>
? ConvexObjectValidatorFromZod<ZodShape>
? ConvexObjectValidatorFromZodOutput<ZodShape>
: Z extends z.ZodUnion<infer T>
? ConvexUnionValidatorFromZod<T>
? ConvexUnionValidatorFromZodOutput<T>
: Z extends z.ZodDiscriminatedUnion<any, infer T>
? VUnion<
ConvexValidatorFromZodOutput<T[number]>["type"],
Expand Down