StefanTerdell / zod-to-json-schema

Converts Zod schemas to Json schemas
ISC License
854 stars 67 forks source link

Add title & example to meta #127

Closed ossamaweb closed 2 months ago

StefanTerdell commented 2 months ago

Hacky

StefanTerdell commented 2 months ago

There are a number of issues and discussions in the Zod repo. I suggest looking for and contributing to those before trying to solve it on this level.

ossamaweb commented 2 months ago

@StefanTerdell Thanks. I saw the feature proposal you have participated in 2548

I don't think it will added soon. Meanwhile I had to implement this hacky solution on my project:

declare module 'zod' {
  interface ZodTypeDef {
    title?: string;
    description?: string;
    example?: unknown;
  }

  interface ZodType {
    title(title: string): this;
    describe(description: string): this;
    example(example: unknown): this;
  }
}

// Extend Zod to add describe, title, and example methods
z.ZodType.prototype.describe = function (description?: string | undefined) {
  this._def.description = description;
  return this;
};

z.ZodType.prototype.title = function (title?: string | undefined) {
  this._def.title = title;
  return this;
};

z.ZodType.prototype.example = function (example?: unknown | undefined) {
  this._def.example = example;
  return this;
};
StefanTerdell commented 2 months ago

I've actually been considering doing something similar for v4! I think we'll leave v3 as it is for now though.

ossamaweb commented 2 months ago

I've actually been considering doing something similar for v4! I think we'll leave v3 as it is for now though.

Good Luck ;)