Skip to content

z.intersection() compiles to unsatisfiable schema #5390

@za-creature

Description

@za-creature

The intersection of two objects

z.toJSONSchema(z.intersection(
    z.object({foo: z.boolean()}),
    z.object({bar: z.boolean()}),
))

compiles to:

{ "allOf": [{
    "type": "object",
    "properties": { "foo": { "type": "boolean" } },
    "required": ["foo"],
    "additionalProperties": false
}, {
    "type": "object",
    "properties": { "bar": { "type": "boolean" } },
    "required": [ "bar" ],
    "additionalProperties": false
}]}

where additionalProperties: false prevents { foo: true, bar: false } from matching at least one of the rules.

.looseObject works around this but isn't strict enough for my taste so for now I'm stuck with extending union cases

Maybe this helps?

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