| 156 | items: YamlRepresentation; |
| 157 | } |
| 158 | export function zodToYAMLObject(schema: z.ZodTypeAny): YamlRepresentation { |
| 159 | // Base case for primitive types |
| 160 | if (schema instanceof z.ZodString) { |
| 161 | return 'string'; |
| 162 | } else if (schema instanceof z.ZodNumber) { |
| 163 | return 'number'; |
| 164 | } else if (schema instanceof z.ZodBoolean) { |
| 165 | return 'boolean'; |
| 166 | } else if (schema instanceof z.ZodNull) { |
| 167 | return 'null'; |
| 168 | } |
| 169 | |
| 170 | // Modified ZodObject case to handle optionals |
| 171 | if (schema instanceof z.ZodObject) { |
| 172 | const shape: Record<string, z.ZodTypeAny> = schema.shape as Record< |
| 173 | string, |
| 174 | z.ZodTypeAny |
| 175 | >; |
| 176 | const yamlObject: YamlObjectRepresentation = {}; |
| 177 | for (const key in shape) { |
| 178 | const fieldSchema = shape[key]; |
| 179 | const optionalSuffix = fieldSchema instanceof z.ZodOptional ? '?' : ''; |
| 180 | if (fieldSchema.description) { |
| 181 | yamlObject[`# ${key} description`] = fieldSchema.description; |
| 182 | } |
| 183 | yamlObject[key + optionalSuffix] = zodToYAMLObject(fieldSchema); |
| 184 | } |
| 185 | return yamlObject; |
| 186 | } |
| 187 | |
| 188 | // Handle arrays |
| 189 | if (schema instanceof z.ZodArray) { |
| 190 | return { |
| 191 | type: 'array', |
| 192 | items: zodToYAMLObject(schema.element), |
| 193 | }; |
| 194 | } |
| 195 | |
| 196 | // records |
| 197 | if (schema instanceof z.ZodRecord) { |
| 198 | const values = zodToYAMLObject(schema.element); |
| 199 | return { |
| 200 | key1: values, |
| 201 | key2: values, |
| 202 | '...': '...', |
| 203 | }; |
| 204 | } |
| 205 | |
| 206 | // Handle union types |
| 207 | if (schema instanceof z.ZodUnion) { |
| 208 | const options = (schema.options as z.ZodTypeAny[]).map((option) => |
| 209 | zodToYAMLObject(option) |
| 210 | ); |
| 211 | return options.join('|'); |
| 212 | } |
| 213 | |
| 214 | // Modified ZodOptional case |
| 215 | if (schema instanceof z.ZodOptional) { |