Skip to content

Commit

Permalink
Release version 0.1.3: Added vertexSchemaToZod function for two-way…
Browse files Browse the repository at this point in the history
… conversion between Zod and Vertex AI schemas, improved documentation with examples, and updated CHANGELOG and package version.
  • Loading branch information
sergeyzenchenko committed Jan 14, 2025
1 parent f6f7109 commit 66bbaa5
Show file tree
Hide file tree
Showing 6 changed files with 539 additions and 4 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.0] - 2024-03-XX
## [0.1.3] - 2024-03-XX

### Added
- `vertexSchemaToZod` function for converting Vertex AI schemas back to Zod schemas
- Two-way conversion support between Zod and Vertex AI schemas
- Improved documentation with examples for both conversion directions

## [0.1.2] - 2024-03-13

### Fixed
- Fixed imports and package structure

## [0.1.0] - 2024-03-12

### Added
- Initial release of `zod-to-vertex-schema`
Expand Down
69 changes: 67 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
[![npm version](https://badge.fury.io/js/@techery%2Fzod-to-vertex-schema.svg)](https://www.npmjs.com/package/@techery/zod-to-vertex-schema)
![CI Status](https://github.com/techery/zod-to-vertex-schema/actions/workflows/pr-checks.yml/badge.svg?branch=main)

Convert [Zod](https://github.com/colinhacks/zod) schemas to [Vertex AI/Gemini](https://cloud.google.com/vertex-ai) compatible schemas. This library helps you define your Vertex AI/Gemini function parameters using Zod's powerful schema definition system.
Convert [Zod](https://github.com/colinhacks/zod) schemas to [Vertex AI/Gemini](https://cloud.google.com/vertex-ai) compatible schemas and back. This library helps you define your Vertex AI/Gemini function parameters using Zod's powerful schema definition system.

Developed by [Techery](https://techery.io).

## Features
- Two-way conversion between Zod and Vertex AI schemas
- Preserves property ordering and descriptions
- Full TypeScript support
- Zero dependencies (except Zod)

## Type Compatibility

| Zod Type | Vertex AI Schema Type | Notes |
Expand Down Expand Up @@ -36,6 +42,61 @@ npm install zod-to-vertex-schema

## Usage

### Converting Zod to Vertex AI Schema

```typescript
import { z } from 'zod';
import { zodToVertexSchema } from 'zod-to-vertex-schema';

// 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);
```

### Converting Vertex AI Schema to Zod

```typescript
import { vertexSchemaToZod } from 'zod-to-vertex-schema';
import { 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: "[email protected]",
roles: ["admin"]
};

const result = zodSchema.safeParse(validUser);
console.log(result.success); // true
```

### Basic Example

```typescript
Expand Down Expand Up @@ -302,6 +363,10 @@ const vertexEventSchema = zodToVertexSchema(eventSchema);

Converts any Zod schema into a Vertex AI/Gemini-compatible schema.

### `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.

### `zodDynamicEnum(values: string[]): z.ZodEnum`

Helper function to create an enum schema from a dynamic array of strings.
Expand All @@ -328,4 +393,4 @@ Contributions are welcome! Please feel free to submit a Pull Request.

## License

MIT
MIT
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@techery/zod-to-vertex-schema",
"version": "0.1.2",
"version": "0.1.3",
"description": "Convert Zod schemas to Vertex AI/Gemini compatible schemas",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { zodToVertexSchema, zodDynamicEnum } from './zod-to-vertex-schema';
export { vertexSchemaToZod } from './vertex-schema-to-zod';
Loading

0 comments on commit 66bbaa5

Please sign in to comment.