Skip to content

Add support for recursion/circular references in Zod #2332

@mmnathan

Description

@mmnathan

Situation
I generate zod schema validators from an openapi 3.0 document, one of the structures has a circular reference to itself.

// Example data model, this wont work because bar implicitly has type 'any', it (`foo`) references itself
const foo = zod.object({
  bar: foo
})
// Example orval output, instead of a self-reference it uses `zod.any`
const  foo = zod.object({
  bar: zod.any(),
})

Expectation
Zod has support for circular references, it would be great if this can be supported (in zod) as well, it works by simply adding a getter for (self/circular) references.

const  foo = zod.object({
  get bar() {
    return foo
  }
})

Please consider adding support for self/circular references, it will greatly improve DX and/or reduce type errors

Metadata

Metadata

Assignees

No one assigned

    Labels

    zodZod related issue

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions