Skip to content

yup schema for recursive inputs overflows the stack; valibot drops input defaults; zod (v3) and myzod place .default() inside optional #1635

Open

Description

@trackoor

Generated input schemas fail at runtime in three cases: recursive input types overflow the stack in yup (the default schema), input field default values are omitted in valibot, and nullable field defaults are placed inside .nullish() / .optional() in zod (on Zod v3) and myzod.

// npm i graphql @graphql-codegen/core @graphql-codegen/typescript graphql-codegen-typescript-validation-schema yup valibot zod@3 myzod
// node repro.mjs   (Node 22.6+ / 24 / 26)
import fs from "node:fs";
import { buildSchema, parse } from "graphql";
import { codegen } from "@graphql-codegen/core";
import * as typescriptPlugin from "@graphql-codegen/typescript";
import * as validationPlugin from "graphql-codegen-typescript-validation-schema";
import * as v from "valibot";

const sdl = `
  input Inner { x: Int! = 5 }
  input Filter { limit: Int = 10 }
  input Node { name: String!, child: Node }
  type Query { ok: Boolean }
`;

const schemaAst = buildSchema(sdl);
const schema = parse(sdl);

async function generate(name) {
  const file = `./gen-${name}.mts`;
  const code = await codegen({
    schema,
    schemaAst,
    documents: [],
    filename: file,
    config: {},
    plugins: [{ typescript: {} }, { validation: { schema: name } }],
    pluginMap: { typescript: typescriptPlugin, validation: validationPlugin },
  });
  fs.writeFileSync(file, code);
  return import(file);
}

const run = (label, fn) => {
  try { console.log(label, "->", JSON.stringify(fn())); }
  catch (e) { console.log(label, "->", `${e.name}: ${e.message.split("\n")[0]}`); }
};

const yupGen = await generate("yup");
run("yup     FilterSchema({})", () => yupGen.FilterSchema().validateSync({}));
run("yup     NodeSchema({ name: 'a' })", () => yupGen.NodeSchema().validateSync({ name: "a" }));

const valibotGen = await generate("valibot");
run("valibot InnerSchema({})", () => v.parse(valibotGen.InnerSchema(), {}));
run("valibot FilterSchema({})", () => v.parse(valibotGen.FilterSchema(), {}));

const zodGen = await generate("zod");
run("zod     InnerSchema({})", () => zodGen.InnerSchema().parse({}));
run("zod     FilterSchema({})", () => zodGen.FilterSchema().parse({}));

const myzodGen = await generate("myzod");
run("myzod   FilterSchema({})", () => myzodGen.FilterSchema().parse({}));

Actual:

yup     FilterSchema({}) -> {"limit":10}
yup     NodeSchema({ name: 'a' }) -> RangeError: Maximum call stack size exceeded
valibot InnerSchema({}) -> ValiError: Invalid key: Expected "x" but received undefined
valibot FilterSchema({}) -> {}
zod     InnerSchema({}) -> {"x":5}
zod     FilterSchema({}) -> {}
myzod   FilterSchema({}) -> {}

Expected (per GraphQL Input Coercion for Input Objects):

yup     FilterSchema({}) -> {"limit":10}
yup     NodeSchema({ name: 'a' }) -> {"name":"a"}
valibot InnerSchema({}) -> {"x":5}
valibot FilterSchema({}) -> {"limit":10}
zod     InnerSchema({}) -> {"x":5}
zod     FilterSchema({}) -> {"limit":10}
myzod   FilterSchema({}) -> {"limit":10}

Likely causes in src/:

  • src/yup/index.ts emits child: yup.lazy(() => NodeSchema()).optional(); during ObjectSchema._cast, Yup resolves yup.lazy even when child is undefined and casts undefined through NodeSchema(), recursing infinitely (yup.lazy(v => v === undefined ? yup.mixed().optional() : NodeSchema()) avoids constructing NodeSchema() on undefined).
  • src/valibot/index.ts does not inspect field.defaultValue, emitting x: v.number() and limit: v.nullish(v.number()) without v.optional(..., defaultValue).
  • src/zod_shared.ts emits z.number().default(10).nullish() (while withNullDefault already emits .nullish().default(null)), and src/myzod/index.ts emits myzod.number().default(10).optional().nullable(); in Zod v3 (including zodImportPath: 'zod/v3' on zod@4, whereas Zod v4's zod / zodv4 unwraps inner defaults) and in myzod, the outer optional wrapper short-circuits undefined before .default(10) runs.

Versions: graphql-codegen-typescript-validation-schema@0.19.0 (also main at fcacab1), @graphql-codegen/core@7.0.0, @graphql-codegen/typescript@7.0.1, graphql@16.15.0, yup@1.7.1, valibot@1.5.0, zod@3.25.76 (and zod@4.6.7 with zodImportPath: 'zod/v3'), myzod@1.12.1, Node.js v26.13.0.

Credit: https://ucsbplse.github.io/

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions