8000 v4: Providing default value for base schema doesn't reference base schema in generated JSON Schema · Issue #4276 · colinhacks/zod · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

v4: Providing default value for base schema doesn't reference base schema in generated JSON Schema #4276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pattobrien opened this issue Apr 24, 2025 · 1 comment

Comments

@pattobrien
Copy link
pattobrien commented Apr 24, 2025

Issue Description

Given the below schemas:

const fooSchema = z.string().meta({
  id: "BaseSchema",
});

const objectWithDefaultFoo = z.object({
  foo: fooSchema.default("bar"),
}).meta({
  id: "ObjectWithDefaultFoo",
});

const jsonSchema = z.toJSONSchema(objectWithDefaultFoo);

I would expect the JSON Schema to be:

{
  "id": "ObjectWithDefaultFoo",
  "type": "object",
  "properties": {
    "foo": {
      "$ref":  "#/$defs/BaseSchema"
      "default": "bar",
    }
  },
  "required": [
    "foo"
  ],
  "$defs": {
    "BaseSchema": {
      "id": "BaseSchema",
      "type": "string"
    }
  }
}

Actual Result

Instead, the "foo" subschema seems to redeclare an inline schema with the id "BaseSchema", rather than pointing to "$defs/BaseSchema".

{
  "id": "ObjectWithDefaultFoo",
  "type": "object",
  "properties": {
    "foo": {
      "id": "BaseSchema", // this is wrong
      "default": "bar",
      "type": "string"
    }
  },
  "required": [
    "foo"
  ],
  "$defs": {
    "BaseSchema": {
      "id": "BaseSchema",
      "type": "string"
    }
  }
}
@pattobrien pattobrien changed the title v4: Giving default value to base schema doesn't use $ref to base schema v4: Providing default value for base schema doesn't reference base schema in generated JSON Schema Apr 24, 2025
@colinhacks
Copy link
Owner

Seems to have been fixed at some point, try upgrading to the latest beta.


PS: Note that you'll need to rewrite your v4 imports to "zod/v4" after upgrading; for more information, see #4371 for details

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0