Skip to content

Unions in intersections still generate with "additionalProperties": false #68

@rijenkii

Description

@rijenkii

See also: #64, #65.

Reproduction:

const left = z.union([
  z.object({ field1: z.number() }),
  z.object({ field3: z.string() }),
]);
const right = z.object({ field2: z.boolean() });
const intersection = z.intersection(left, right);
const schema = zodToJsonSchema(intersection);

Generates the following schema:

{
  "allOf": [
    {
      "anyOf": [
        {
          "type": "object",
          "properties": { "field1": { "type": "number" } },
          "required": ["field1"],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": { "field3": { "type": "string" } },
          "required": ["field3"],
          "additionalProperties": false
        }
      ]
    },
    {
      "type": "object",
      "properties": { "field2": { "type": "boolean" } },
      "required": ["field2"]
    }
  ],
  "$schema": "http://json-schema.org/draft-07/schema#"
}

Because of "additionalProperties": false, no object can be successfully validated against this schema:
https://www.jsonschemavalidator.net/s/ddO3vtM2

Compared to schema with removed additionalProperties:
https://www.jsonschemavalidator.net/s/UO8HV6Lz

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions