8000 v4: `z.toJSONSchema` incorrectly generates required properties for `default` values · Issue #4279 · colinhacks/zod · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
v4: z.toJSONSchema incorrectly generates required properties for default values #4279
Closed
@pattobrien

Description

@pattobrien

Description

Given the below schemas:

const foo = z.object({
  prop: z.string().default("fooPropDefault"),
});
const bar = z.interface({
  prop1: z.string().default("barProp1Default"),
  "prop2?": z.string().default("barProp2Default"),
});

const fooInputJsonSchema = z.toJSONSchema(foo, { io: "input" });
const fooOutputJsonSchema = z.toJSONSchema(foo, { io: "output" });
const barInputJsonSchema = z.toJSONSchema(bar, { io: "input" });
const barOutputJsonSchema = z.toJSONSchema(bar, { io: "output" });

When generating to JSON Schema (via z.toJSONSchema(...)), each optional field is expected to not be required in input schema, and required for the output schema.

Actual Result

// foo-input.json
{
  "type": "object",
  "properties": {
    "prop": {
      "type": "string",
      "default": "fooPropDefault"
    }
  },
  "required": [
    "prop" // incorrect
  ]
}

// bar-input.json
{
  "type": "object",
  "properties": {
    "prop1": {
      "type": "string",
      "default": "barProp1Default"
    },
    "prop2": {
      "type": "string",
      "default": "barProp2Default"
    }
  },
  "required": [
    "prop1" // incorrect
  ]
}

// bar-output.json
{
  "type": "object",
  "properties": {
    "prop1": {
      "type": "string",
      "default": "barProp1Default"
    },
    "prop2": {
      "type": "string",
      "default": "barProp2Default"
    }
  },
  "required": [
    "prop1",
    // missing prop2
  ]
}

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