Skip to content

Commit e1f8f70

Browse files
committed
fix: cover default schema in zod schema fixer
1 parent 5cf1cef commit e1f8f70

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

src/core/zod-to-openapi.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type ZodObject, type ZodType, z } from "zod";
1+
import { type ZodDefault, type ZodObject, type ZodType, z } from "zod";
22
import type { SchemaObject } from "@omer-x/json-schema-types";
33

44
function alterSchema(schema: ZodType<unknown>, newShape: ZodType<unknown>): ZodType<unknown> {
@@ -14,6 +14,10 @@ function fixSchema(schema: ZodType<unknown>): ZodType<unknown> {
1414
case "nullable": return alterSchema(schema, fixSchema(schema.unwrap()).nullable());
1515
case "optional": return alterSchema(schema, fixSchema(schema.unwrap()).optional());
1616
case "readonly": return alterSchema(schema, fixSchema(schema.unwrap()).readonly());
17+
case "default": {
18+
const defaultValue = (schema as unknown as ZodDefault)._zod.def.defaultValue;
19+
return alterSchema(schema, fixSchema(schema.unwrap()).default(defaultValue));
20+
}
1721
case "array": return alterSchema(schema, fixSchema(schema.unwrap()).array());
1822
default: throw new Error(`${schema._zod.def.type} type is not covered in fixSchema (@omer-x/next-openapi-json-generator")`);
1923
}

0 commit comments

Comments
 (0)