ai-functions
Version:
A powerful TypeScript library for building AI-powered applications with template literals and structured outputs
30 lines • 1.09 kB
JavaScript
import { z } from 'zod';
export function convertToZodSchema(schema) {
const zodSchema = {};
for (const [key, value] of Object.entries(schema)) {
zodSchema[key] = convertValue(value);
}
return z.object(zodSchema);
}
function convertValue(value) {
if (Array.isArray(value)) {
// If it's an array, create a string array schema with the first item as description
return z.array(z.string().describe(value[0] || ''));
}
else if (typeof value === 'string') {
// Check if the string contains enum values (separated by |)
if (value.includes('|')) {
const enumValues = value.split('|').map(v => v.trim());
return z.enum(enumValues);
}
// Otherwise, create a string schema with the value as description
return z.string().describe(value);
}
else if (typeof value === 'object') {
// If it's an object, recursively convert it
return convertToZodSchema(value);
}
// Fallback to string
return z.string();
}
//# sourceMappingURL=schema-converter.js.map