8000 v4: toJSONSchema: `_ref` complicates overrides · Issue #4275 · colinhacks/zod · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
v4: toJSONSchema: _ref complicates overrides #4275
Closed
@RobinTail

Description

@RobinTail

core v0.9.0
Let's say there is a schema

const schema = z.union([
    z.string().date(),
    z.string().datetime(),
    z.string().datetime({ local: true }),
  ]).transform((str) => new Date(str))
    .pipe(z.date())
    .brand("dateIn")

When doing z.toJSONSchema() with override hook, this is what jsonSchema argument receives:

{
  "_ref": {
    "_ref": {
      "_ref": {
        "anyOf": [
          {
            "type": "string",
            "format": "date",
            "pattern": "^((\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-((0[13578]|1[02])-(0[1-9]|[12]\\d|3[01])|(0[469]|11)-(0[1-9]|[12]\\d|30)|(02)-(0[1-9]|1\\d|2[0-8])))$"
          },
          {
            "type": "string",
            "format": "date-time",
            "pattern": "^((\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-((0[13578]|1[02])-(0[1-9]|[12]\\d|3[01])|(0[469]|11)-(0[1-9]|[12]\\d|30)|(02)-(0[1-9]|1\\d|2[0-8])))T([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(\\.\\d+)?(Z)$"
          },
          {
            "type": "string",
            "format": "date-time",
            "pattern": "^((\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-((0[13578]|1[02])-(0[1-9]|[12]\\d|3[01])|(0[469]|11)-(0[1-9]|[12]\\d|30)|(02)-(0[1-9]|1\\d|2[0-8])))T([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(\\.\\d+)?(Z?)$"
          }
        ]
      }
    }
  }
}

so, everything is packed in _ref multiple times. The property is optional, so that it may be not be there.
It complicates making overrides.

Could all those _refs be resolved or "unrefed" BEFORE handling jsonSchema to the override hook, @colinhacks ?

For comparison: there was no such issue with core 0.8.1

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