Skip to content

sauravexodus/zod-to-baml

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Zod to BAML

Zod to BAML Converter

A TypeScript library that converts Zod schemas to BAML format.

Installation

npm install zod-baml-converter

Usage

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
    }
  }
}

Supported Types

  • Primitive types (string, number, boolean)
  • Arrays (list)
  • Objects (struct)
  • Enums
  • Optional fields
  • Nullable fields
  • Union types

Development

# Install dependencies
npm install

# Build the project
npm run build

# Run tests
npm test

# Run example
npm start

License

MIT

About

Efficiently convert your zod schemas to BAML

Resources

Stars

Watchers

Forks

Packages

No packages published