@mchen-lab/string-to-zod
v0.1.0
Published
Convert string-schema syntax to Zod schemas for LLM structured output
Maintainers
Readme
@mchen-lab/string-to-zod
Convert string-schema syntax to Zod schemas for LLM structured output.
Installation
npm install @mchen-lab/string-to-zodUsage
import { stringToZodSchema } from '@mchen-lab/string-to-zod';
// Basic types
const userSchema = stringToZodSchema("{name:str, age:int, active:bool}");
// Optional fields
const profileSchema = stringToZodSchema("{bio:string?, website:url?}");
// Constraints
const productSchema = stringToZodSchema("{name:string(min=1, max=100), price:number(min=0)}");
// Arrays
const tagsSchema = stringToZodSchema("{tags:[string]}");
// Nested objects
const complexSchema = stringToZodSchema("{user:{name:str}, items:[{id:int, qty:int}]}");
// Enums
const statusSchema = stringToZodSchema("{status:enum(pending, active, completed)}");
// Defaults and descriptions
const configSchema = stringToZodSchema("{enabled:bool=true | Feature flag, count:int=0}");String Schema Syntax
| Type | Example | Description |
|------|---------|-------------|
| str / string | {name:str} | String field |
| int / integer | {age:int} | Integer number |
| num / number / float | {price:num} | Float number |
| bool / boolean | {active:bool} | Boolean |
| url | {website:url} | URL string |
| email | {contact:email} | Email string |
| date | {created:date} | Date string |
| [type] | {tags:[string]} | Array of type |
| {...} | {user:{name:str}} | Nested object |
| enum(...) | {status:enum(a, b)} | Enum values |
| ? suffix | {bio:str?} | Optional field |
| =value | {count:int=0} | Default value |
| \| desc | {name:str \| User name} | Field description |
API
stringToZodSchema(schemaStr: string): ZodTypeAny
Converts a string schema to a Zod schema.
parseSchema(schemaStr: string): ParsedSchema
Parses a string schema into an intermediate AST (useful for debugging).
License
MIT
