Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,29 @@ export class OpenAPIParser {

private convertSchemaToZod(schema: SchemaObject): z.ZodSchema<any> {
if (schema.enum) {
const cleanedEnum = schema.enum.map((val: any) =>
typeof val === "string" ? val.replace(/^"|"$/g, "") : val,
const allStrings = schema.enum.every(
(val: any) => typeof val === "string",
);
if (allStrings) {
const cleanedEnum = schema.enum.map((val: string) =>
val.replace(/^"|"$/g, ""),
);
return z.enum(cleanedEnum as [string, ...string[]]);
}
// Non-string enums use z.union of literals
if (schema.enum.length === 0) {
return z.never();
}
if (schema.enum.length === 1) {
return z.literal(schema.enum[0]);
}
return z.union(
schema.enum.map((val: any) => z.literal(val)) as [
z.ZodLiteral<any>,
z.ZodLiteral<any>,

@dp-franklin dp-franklin Feb 19, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi: I assume that the z.union type requires at least two items in the array, but only one item is actually required at runtime. (0 will throw)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ty, addressed in the latest force push

...z.ZodLiteral<any>[],
],
);
return z.enum(cleanedEnum as [string, ...string[]]);
}

switch (schema.type) {
Expand Down