### Convert Zod Schema for Convex Function Arguments Source: https://context7.com/gbouteiller/zod-convex/llms.txt Use `convexArgsFrom` to convert Zod schemas into Convex argument validators. This is useful for defining the expected structure and types of arguments passed to your Convex mutations and queries. ```typescript import { convexArgsFrom, zid } from "zod-convex"; import { z } from "zod"; // Define argument schema for a Convex mutation const createPostArgs = { authorId: zid("users"), title: z.string().min(1).max(200), content: z.string(), tags: z.array(z.string()).optional(), status: z.enum(["draft", "published"]), }; const convexArgs = convexArgsFrom(createPostArgs); // Result: v.object({ // authorId: v.id("users"), // title: v.string(), // content: v.string(), // tags: v.optional(v.array(v.string())), // status: v.union(v.literal("draft"), v.literal("published")) // }) // Use in Convex function definition // export const createPost = mutation({ // args: convexArgsFrom(createPostArgs), // handler: async (ctx, args) => { ... } // }); ``` -------------------------------- ### Unsupported Zod Types for Convex Conversion Source: https://context7.com/gbouteiller/zod-convex/llms.txt Demonstrates Zod types that will throw errors when converted to Convex. Use numeric timestamps for dates, unions for intersections, and avoid unsupported types like Map, Set, File, Promise, Never, Void, Undefined, custom, and transform. ```typescript import { convexFrom } from "zod-convex"; import { z } from "zod"; // These will throw errors: // JavaScript Date objects (use number timestamps instead) convexFrom(z.date()); // Error: 'Unsupported Zod type "date" for conversion to Convex' // Map and Set collections convexFrom(z.map(z.string(), z.number())); // Error: 'Unsupported Zod type "map" for conversion to Convex' convexFrom(z.set(z.string())); // Error: 'Unsupported Zod type "set" for conversion to Convex' // File uploads convexFrom(z.file()); // Error: 'Unsupported Zod type "file" for conversion to Convex' // Intersection types convexFrom(z.intersection(z.object({ a: z.string() }), z.object({ b: z.number() }))); // Error: 'Unsupported Zod type "intersection" for conversion to Convex' // Promise wrappers convexFrom(z.promise(z.string())); // Error: 'Unsupported Zod type "promise" for conversion to Convex' // Never and void types convexFrom(z.never()); // Error: 'Unsupported Zod type "never" for conversion to Convex' convexFrom(z.void()); // Error: 'Unsupported Zod type "void" for conversion to Convex' convexFrom(z.undefined()); // Error: 'Unsupported Zod type "undefined" for conversion to Convex' // Custom validators convexFrom(z.custom()); // Error: 'Unsupported Zod type "custom" for conversion to Convex' // Transform functions convexFrom(z.transform((val) => val.toString())); // Error: 'Unsupported Zod type "transform" for conversion to Convex' ``` -------------------------------- ### Create Convex ID Validator with zid Source: https://context7.com/gbouteiller/zod-convex/llms.txt Use `zid` to create Zod schemas for Convex document IDs. Convert these schemas to Convex validators using `convexFrom` for use in function arguments or table definitions. ```typescript import { zid, convexFrom } from "zod-convex"; import { v } from "convex/values"; // Create a Zod schema for a Convex document ID const userIdSchema = zid("users"); const postIdSchema = zid("posts"); // Convert to Convex validators const userIdValidator = convexFrom(userIdSchema); // Equivalent to: v.id("users") const postIdValidator = convexFrom(postIdSchema); // Equivalent to: v.id("posts") // Use in a Convex function argument schema const argsSchema = { userId: zid("users"), postId: zid("posts"), }; const convexArgs = convexFrom(argsSchema); // Result: v.object({ userId: v.id("users"), postId: v.id("posts") }) ``` -------------------------------- ### Handle Zod Wrapper Types with Convex Validators Source: https://context7.com/gbouteiller/zod-convex/llms.txt The `zod-convex` library intelligently handles Zod's wrapper types, preserving optionality, nullability, default values, and other modifiers when converting to Convex validators. ```typescript import { convexFrom } from "zod-convex"; import { z } from "zod"; // Optional types convexFrom(z.string().optional()); // v.optional(v.string()) convexFrom(z.optional(z.number())); // v.optional(v.float64()) // Nullable types convexFrom(z.string().nullable()); // v.union(v.string(), v.null()) convexFrom(z.nullable(z.boolean())); // v.union(v.boolean(), v.null()) // Nullish types (optional + nullable) convexFrom(z.string().nullish()); // v.optional(v.union(v.string(), v.null())) // Default values (removes optionality in Convex) convexFrom(z.boolean().optional().default(false)); // v.boolean() convexFrom(z.string().default("unknown")); // v.string() // Prefault (pre-parse default) convexFrom(z.number().optional().prefault(0)); // v.float64() // Catch (fallback on parse error) convexFrom(z.number().catch(0)); // v.float64() // Lazy evaluation (for recursive types) convexFrom(z.lazy(() => z.string())); // v.string() // Readonly (passthrough) convexFrom(z.readonly(z.object({ x: z.number() }))); // v.object({ x: v.float64() }) // Coercion (uses output type) convexFrom(z.coerce.string()); // v.string() convexFrom(z.coerce.number()); // v.float64() // Pipe transformations (uses output type) convexFrom(z.pipe(z.string(), z.stringbool())); // v.boolean() // Non-optional (removes optional wrapper) convexFrom(z.nonoptional(z.string().nullish())); // v.union(v.string(), v.null()) ``` -------------------------------- ### Convert Zod Schema for Convex Table Definitions Source: https://context7.com/gbouteiller/zod-convex/llms.txt Utilize `convexTableFrom` to transform Zod schemas into Convex table schema definitions. This function is ideal for setting up your database table structures, ensuring data integrity through Zod validation. ```typescript import { convexTableFrom, zid } from "zod-convex"; import { z } from "zod"; // Define a user table schema const userTableSchema = { email: z.email(), name: z.string(), avatarUrl: z.url().optional(), role: z.enum(["admin", "member", "guest"]), createdAt: z.number(), metadata: z.record(z.string(), z.any()).optional(), }; const convexUserTable = convexTableFrom(userTableSchema); // Result: v.object({ // email: v.string(), // name: v.string(), // avatarUrl: v.optional(v.string()), // role: v.union(v.literal("admin"), v.literal("member"), v.literal("guest")), // createdAt: v.float64(), // metadata: v.optional(v.record(v.string(), v.any())) // }) // Define a post table with relationships const postTableSchema = { authorId: zid("users"), title: z.string(), body: z.string(), publishedAt: z.number().nullable(), tags: z.array(z.string()), }; const convexPostTable = convexTableFrom(postTableSchema); // Result: v.object({ // authorId: v.id("users"), // title: v.string(), // body: v.string(), // publishedAt: v.union(v.float64(), v.null()), // tags: v.array(v.string()) // }) ``` -------------------------------- ### Convert Zod Schema to Convex Validator with convexFrom Source: https://context7.com/gbouteiller/zod-convex/llms.txt The `convexFrom` function transforms Zod schemas into Convex validators. It supports primitive types, string formats, literals, enums, objects, arrays, unions, discriminated unions, records, and tuples. ```typescript import { convexFrom } from "zod-convex"; import { z } from "zod"; import { v } from "convex/values"; // Primitive types convexFrom(z.string()); // v.string() convexFrom(z.number()); // v.float64() convexFrom(z.boolean()); // v.boolean() convexFrom(z.bigint()); // v.int64() convexFrom(z.null()); // v.null() convexFrom(z.any()); // v.any() // String format types (all convert to v.string()) convexFrom(z.email()); // v.string() convexFrom(z.url()); // v.string() convexFrom(z.uuid()); // v.string() // Literal and enum types convexFrom(z.literal("active")); // v.literal("active") convexFrom(z.literal(["a", "b"])); // v.union(v.literal("a"), v.literal("b")) convexFrom(z.enum(["pending", "approved"])); // v.union(v.literal("pending"), v.literal("approved")) // Object types convexFrom(z.object({ name: z.string(), age: z.number(), email: z.email().optional(), })); // Result: v.object({ name: v.string(), age: v.float64(), email: v.optional(v.string()) }) // Array types convexFrom(z.array(z.string())); // v.array(v.string()) convexFrom(z.string().array()); // v.array(v.string()) // Union types convexFrom(z.union([z.string(), z.number()])); // v.union(v.string(), v.float64()) // Discriminated unions convexFrom(z.discriminatedUnion("type", [ z.object({ type: z.literal("text"), content: z.string() }), z.object({ type: z.literal("image"), url: z.url() }), ])); // Result: v.union( // v.object({ type: v.literal("text"), content: v.string() }), // v.object({ type: v.literal("image"), url: v.string() }) // ) // Record types convexFrom(z.record(z.string(), z.number())); // v.record(v.string(), v.float64()) // Tuple types (converted to arrays with union elements) convexFrom(z.tuple([z.string(), z.number()])); // v.array(v.union(v.string(), v.float64())) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.