Skip to content

Stack overflow when using toType on self-referential/recursive schema #566

@PowerSupply

Description

@PowerSupply

Reproduction:

const recursiveSchema = z.object({
    id: z.string(),
    get children() {
        return z.array(recursiveSchema);
    },
});

console.log(zx.toType(recursiveSchema));    // RangeError: Maximum call stack size exceeded

Probably related to #540 as well.

One possible solution to this could be to require a recursive schema to have a description (exist in the z.globalRegistry) so that you can use the description string as a value to break the infinite loop.

The proposes solution would also be nice to have for schemas that are not recursive, but contain other schemas, so that you could list them separately (using their description as placeholder where they are used in other schemas) instead of inlining all types.

I could try to make a PR if you agree with the proposed solution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions