-
code const baseCollectionSchema = z.object({
id: z.string(),
name: z.string(),
slug: z.string().or(z.null()),
path: z.string(),
depth: z.number(),
numchild: z.number(),
});
type Collection = z.infer<typeof baseCollectionSchema> & {
children?: Collection[];
};
const collectionSchema: z.ZodType<Collection> = baseCollectionSchema
.extend({
children: z
.lazy(() => z.array(collectionSchema))
.optional()
.openapi({
type: 'array',
items: {
$ref: '#/components/schemas/Collection',
},
}),
})
.openapi({ ref: 'Collection' }); error
|
Beta Was this translation helpful? Give feedback.
Answered by
samchungy
Jul 7, 2025
Replies: 2 comments 3 replies
-
G'day. I tried to reproduce this but couldn't quite do it: import { z } from "zod";
import { createSchema } from "zod-openapi";
import "zod-openapi/extend";
const baseCollectionSchema = z.object({
id: z.string(),
name: z.string(),
slug: z.string().or(z.null()),
path: z.string(),
depth: z.number(),
numchild: z.number(),
});
type Collection = z.infer<typeof baseCollectionSchema> & {
children?: Collection[];
};
const collectionSchema: z.ZodType<Collection> = baseCollectionSchema
.extend({
children: z.lazy(() => z.array(collectionSchema)).optional(),
})
.openapi({ ref: "Collection" });
console.log(JSON.stringify(createSchema(collectionSchema), null, 2)); {
"schema": {
"$ref": "#/components/schemas/Collection"
},
"components": {
"Collection": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"slug": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
]
},
"path": {
"type": "string"
},
"depth": {
"type": "number"
},
"numchild": {
"type": "number"
},
"children": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Collection"
}
}
},
"required": [
"id",
"name",
"slug",
"path",
"depth",
"numchild"
]
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
What versions are you running? |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lazy is now natively supported as of v5