Zod to BAML
A TypeScript library that converts Zod schemas to BAML format.
npm install zod-baml-converter
import { z } from "zod";
import { ZodToBAMLConverter } from "zod-baml-converter";
// Define your Zod schema
const userSchema = z.object({
id: z.string(),
name: z.string(),
age: z.number(),
email: z.string().email(),
isActive: z.boolean(),
role: z.enum(["admin", "user", "guest"]),
tags: z.array(z.string()),
profile: z.object({
bio: z.string().optional(),
avatar: z.string().nullable(),
settings: z.object({
theme: z.enum(["light", "dark"]),
notifications: z.boolean(),
}),
}),
});
// Convert to BAML
const converter = new ZodToBAMLConverter();
const bamlSchema = converter.convert(userSchema);
console.log(bamlSchema);
This will output the following BAML schema:
struct {
id: string
name: string
age: float
email: string
isActive: boolean
role: enum { "admin", "user", "guest" }
tags: list<string>
profile: struct {
bio: optional<string>
avatar: nullable<string>
settings: struct {
theme: enum { "light", "dark" }
notifications: boolean
}
}
}
- Primitive types (string, number, boolean)
- Arrays (list)
- Objects (struct)
- Enums
- Optional fields
- Nullable fields
- Union types
# Install dependencies
npm install
# Build the project
npm run build
# Run tests
npm test
# Run example
npm start
MIT