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
Closed
@pattobrien

Description

@pattobrien

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"
    }
  }
}

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

      0