### Install zod-to-vertex-schema Source: https://github.com/techery/zod-to-vertex-schema/blob/main/README.md Installs the library using npm. This is the primary method for adding the package to your project. ```bash npm install zod-to-vertex-schema ``` -------------------------------- ### Handle Optional and Nullable Fields Source: https://github.com/techery/zod-to-vertex-schema/blob/main/README.md Demonstrates how the library handles Zod's optional() and nullable() modifiers when converting to Vertex AI schemas, affecting the 'required' and 'nullable' properties. ```typescript import { zodToVertexSchema, SchemaType } from 'zod-to-vertex-schema'; import { z } from 'zod'; const profileSchema = z.object({ username: z.string(), displayName: z.string().optional(), // Will be marked as not required bio: z.string().nullable(), // Will be marked as nullable lastSeen: z.date().optional().nullable(), // Both optional and nullable }); const vertexProfileSchema = zodToVertexSchema(profileSchema); // Resulting Vertex AI schema: // { // type: "OBJECT", // properties: { // username: { type: "STRING" }, // displayName: { type: "STRING" }, // bio: { type: "STRING", nullable: true }, // lastSeen: { type: "STRING", format: "date", nullable: true } // }, // required: ["username"], // propertyOrdering: ["username", "displayName", "bio", "lastSeen"] // } ``` -------------------------------- ### Zod to Vertex AI: Date and Format Validation Source: https://github.com/techery/zod-to-vertex-schema/blob/main/README.md Demonstrates how Zod's `.date()` and `.string().datetime()` types are mapped to Vertex AI schema formats for dates and date-times, respectively. ```typescript const eventSchema = z.object({ title: z.string(), startDate: z.date(), // Will be converted to date format endDate: z.date(), createdAt: z.string().datetime(), // Will be converted to date-time format }); const vertexEventSchema = zodToVertexSchema(eventSchema); ``` ```json { "type": "OBJECT", "properties": { "title": { "type": "STRING" }, "startDate": { "type": "STRING", "format": "date" }, "endDate": { "type": "STRING", "format": "date" }, "createdAt": { "type": "STRING", "format": "date-time" } }, "required": ["title", "startDate", "endDate", "createdAt"], "propertyOrdering": ["title", "startDate", "endDate", "createdAt"] } ``` -------------------------------- ### Zod to Vertex AI Schema Conversion Source: https://github.com/techery/zod-to-vertex-schema/blob/main/README.md Provides functions to convert schemas between Zod and Vertex AI/Gemini formats. `zodToVertexSchema` transforms Zod schemas into Vertex AI compatible schemas, while `vertexSchemaToZod` converts Vertex AI schemas back to Zod. The conversion supports many Zod features but has limitations regarding default values, recursive schemas, and specific Zod constructs. ```APIDOC zodToVertexSchema(schema: z.ZodTypeAny): VertexSchema Converts any Zod schema into a Vertex AI/Gemini-compatible schema. - Parameters: - schema: The Zod schema object to convert. - Returns: - A Vertex AI/Gemini schema object. vertexSchemaToZod(schema: VertexSchema): z.ZodTypeAny Converts a Vertex AI/Gemini schema back into a Zod schema. Supports all features that are available in the Vertex AI schema format. - Parameters: - schema: The Vertex AI/Gemini schema object to convert. - Returns: - A Zod schema object. ``` -------------------------------- ### Zod to Vertex AI: Unions and Discriminated Unions Source: https://github.com/techery/zod-to-vertex-schema/blob/main/README.md Illustrates the conversion of Zod's union and discriminated union types into Vertex AI's `anyOf` structure. This is useful for representing schemas that can take one of several forms. ```typescript // Simple union const resultSchema = z.union([ z.object({ success: z.literal(true), data: z.string() }), z.object({ success: z.literal(false), error: z.string() }), ]); // Discriminated union const shapeSchema = z.discriminatedUnion('type', [ z.object({ type: z.literal('circle'), radius: z.number() }), z.object({ type: z.literal('rectangle'), width: z.number(), height: z.number() }), ]); const vertexResultSchema = zodToVertexSchema(resultSchema); const vertexShapeSchema = zodToVertexSchema(shapeSchema); ``` ```json // For resultSchema: { "anyOf": [ { "type": "OBJECT", "properties": { "success": { "type": "STRING", "enum": ["true"] }, "data": { "type": "STRING" } }, "required": ["success", "data"], "propertyOrdering": ["success", "data"] }, { "type": "OBJECT", "properties": { "success": { "type": "STRING", "enum": ["false"] }, "error": { "type": "STRING" } }, "required": ["success", "error"], "propertyOrdering": ["success", "error"] } ] } // For shapeSchema: { "anyOf": [ { "type": "OBJECT", "properties": { "type": { "type": "STRING", "enum": ["circle"] }, "radius": { "type": "NUMBER" } }, "required": ["type", "radius"], "propertyOrdering": ["type", "radius"] }, { "type": "OBJECT", "properties": { "type": { "type": "STRING", "enum": ["rectangle"] }, "width": { "type": "NUMBER" }, "height": { "type": "NUMBER" } }, "required": ["type", "width", "height"], "propertyOrdering": ["type", "width", "height"] } ] } ``` -------------------------------- ### Convert Vertex AI Schema to Zod Schema Source: https://github.com/techery/zod-to-vertex-schema/blob/main/README.md Converts a Vertex AI schema object back into a Zod schema object. This allows for validation of Vertex AI generated schemas using Zod. ```typescript import { vertexSchemaToZod, SchemaType } from 'zod-to-vertex-schema'; // Your Vertex AI schema const vertexSchema = { type: SchemaType.OBJECT, properties: { name: { type: SchemaType.STRING }, age: { type: SchemaType.INTEGER, minimum: 0 }, email: { type: SchemaType.STRING }, roles: { type: SchemaType.ARRAY, items: { type: SchemaType.STRING, enum: ["admin", "user"] } } }, required: ["name", "age", "email", "roles"], propertyOrdering: ["name", "age", "email", "roles"] }; // Convert back to Zod schema const zodSchema = vertexSchemaToZod(vertexSchema); // Now you can use it for validation const validUser = { name: "John", age: 30, email: "john@example.com", roles: ["admin"] }; const result = zodSchema.safeParse(validUser); console.log(result.success); // true ``` -------------------------------- ### Create Dynamic Enum Schema Source: https://github.com/techery/zod-to-vertex-schema/blob/main/README.md A helper function to create a Zod enum schema directly from a dynamic array of string values. This is useful when the possible enum values are not known at static definition time. ```typescript const roles = ['admin', 'user', 'guest']; const roleSchema = zodDynamicEnum(roles); ``` -------------------------------- ### Zod to Vertex AI: Complex Nested Objects Source: https://github.com/techery/zod-to-vertex-schema/blob/main/README.md Shows the conversion of a Zod schema with deeply nested objects and arrays into its Vertex AI schema representation. This includes defining structures for customer data and order items. ```typescript const addressSchema = z.object({ street: z.string(), city: z.string(), country: z.string(), postalCode: z.string(), }); const orderSchema = z.object({ orderId: z.string(), customer: z.object({ id: z.string(), name: z.string(), addresses: z.array(addressSchema), }), items: z.array(z.object({ productId: z.string(), quantity: z.number().int().min(1), price: z.number().min(0), })), status: z.enum(['pending', 'processing', 'shipped', 'delivered']), createdAt: z.date(), }); const vertexOrderSchema = zodToVertexSchema(orderSchema); ``` ```json { "type": "OBJECT", "properties": { "orderId": { "type": "STRING" }, "customer": { "type": "OBJECT", "properties": { "id": { "type": "STRING" }, "name": { "type": "STRING" }, "addresses": { "type": "ARRAY", "items": { "type": "OBJECT", "properties": { "street": { "type": "STRING" }, "city": { "type": "STRING" }, "country": { "type": "STRING" }, "postalCode": { "type": "STRING" } }, "required": ["street", "city", "country", "postalCode"], "propertyOrdering": ["street", "city", "country", "postalCode"] } } }, "required": ["id", "name", "addresses"], "propertyOrdering": ["id", "name", "addresses"] }, "items": { "type": "ARRAY", "items": { "type": "OBJECT", "properties": { "productId": { "type": "STRING" }, "quantity": { "type": "INTEGER", "minimum": 1 }, "price": { "type": "NUMBER", "minimum": 0 } }, "required": ["productId", "quantity", "price"], "propertyOrdering": ["productId", "quantity", "price"] } }, "status": { "type": "STRING", "enum": ["pending", "processing", "shipped", "delivered"] }, "createdAt": { "type": "STRING", "format": "date" } }, "required": ["orderId", "customer", "items", "status", "createdAt"], "propertyOrdering": ["orderId", "customer", "items", "status", "createdAt"] } ``` -------------------------------- ### Convert Zod Schema to Vertex AI Schema Source: https://github.com/techery/zod-to-vertex-schema/blob/main/README.md Converts a Zod schema object into a Vertex AI compatible schema object. This is useful for defining function parameters for Vertex AI models. ```typescript import { zodToVertexSchema, SchemaType } from 'zod-to-vertex-schema'; import { z } from 'zod'; // Define your Zod schema const userSchema = z.object({ name: z.string(), age: z.number().int().min(0), email: z.string().email(), roles: z.array(z.enum(['admin', 'user'])), }); // Convert to Vertex AI schema const vertexSchema = zodToVertexSchema(userSchema); // Resulting Vertex AI schema: // { // type: "OBJECT", // properties: { // name: { type: "STRING" }, // age: { type: "INTEGER", minimum: 0 }, // email: { type: "STRING" }, // roles: { // type: "ARRAY", // items: { type: "STRING", enum: ["admin", "user"] } // } // }, // required: ["name", "age", "email", "roles"], // propertyOrdering: ["name", "age", "email", "roles"] // } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.