Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect $ref for self-referencing types #99

Closed
LaserUnicorns opened this issue Dec 13, 2023 · 1 comment
Closed

Incorrect $ref for self-referencing types #99

LaserUnicorns opened this issue Dec 13, 2023 · 1 comment

Comments

@LaserUnicorns
Copy link

Given a self-referencing type:

const Test = z.lazy(() =>
  z.object({
    id: z.string(),
    test: z.optional(Test),
  }),
)

When using zodToJsonSchema, the produced JSON schema is as follows:

{
  "type": "object",
  "properties": {
    "id": {
      "type": "string"
    },
    "test": {
      "$ref": "#/" // <- this one
    }
  },
  "required": [
    "id"
  ],
  "additionalProperties": false,
  "$schema": "http://json-schema.org/draft-07/schema#"
}

The issue arises with the "$ref" property under "test," which currently contains #/. According to RFC6901, this points to an empty string property:

{
  "": "this"
}

To adhere to the RFC, the $ref property should simply contain #, pointing to the entire document.

@StefanTerdell
Copy link
Owner

Fair. Fix published in 3.22.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants