StefanTerdell / zod-to-json-schema

Converts Zod schemas to Json schemas
ISC License
917 stars 76 forks source link

Ability to specify title of an object #140

Closed ryan0x44 closed 2 months ago

ryan0x44 commented 2 months ago

I'm not sure how you'd approach annotating the zod schema with the necessary data, but it would be great to be able to set the title field on the JSON Schema object.

e.g. "title": "TitleHere", in the context of zod-to-json-schema README 'Expected output' example:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$ref": "#/definitions/mySchema",
  "definitions": {
    "mySchema": {
      "description": "My neat object schema",
      "type": "object",
      "title": "TitleHere",
      "properties": {
        "myString": {
          "type": "string",
          "minLength": 5
        },
        "myUnion": {
          "type": ["number", "boolean"]
        },
      },
      "additionalProperties": false,
      "required": ["myString", "myUnion"]
    }
  }
}

Currently I use the Zod .describe() method to set the description, then do a find and replace on the JSON Schema output for the "description" field, which is very hacky and preclude me from using the description field.

Official JSON Schema docs about annotations such as title can be found here: https://json-schema.org/understanding-json-schema/reference/annotations

StefanTerdell commented 2 months ago

I'm not sure how you'd approach annotating the zod schema with the necessary data

That's the issue, there is no canonical way of doing this. Maybe in v4, I guess we'll see

Try the nameStrategy or override options.