fabien0102 / ts-to-zod

Generate zod schemas from typescript types/interfaces
MIT License
1.2k stars 65 forks source link

Generated schema doesn’t match types #273

Open lensbart opened 3 hours ago

lensbart commented 3 hours ago

Bug description

npm i -D openapi-typescript ts-to-zod

In package.json:

{
  "scripts": {
    "fetch-openapi-types": "openapi-typescript https://developers.zoom.us/api-hub/video-sdk/events/webhooks.json --alphabetize --export-type --output ./webhooks-types.ts",
    "convert-types-to-zod": "ts-to-zod ./webhooks-types.ts ./webhooks-schema.ts",
  }
}
npm run fetch-openapi-types
npm run convert-types-to-zod

Output:

✖ Validating generated types
 ›   Error: 'webhooksSchema' is not compatible with 
 ›   'webhooks':
 ›   Argument of type '{ "session.alert": { [x: string]: any;
 ›    parameters?: unknown; get?: unknown; put?: unknown; 
 ›   post?: unknown; "session.alert"?: unknown; delete?: 
 ›   unknown; options?: unknown; head?: unknown; patch?: 
 ›   unknown; trace?: unknown; }; "session.ended": { [x: 
 ›   string]: any; parameters?: unknown; get?: unknown; put?:
 ›    unknown; post...' is not assignable to parameter of 
 ›   type 'webhooks'.
 ›     The types of '"session.alert".parameters' are 
 ›   incompatible between these types.
 ›       Type 'unknown' is not assignable to type '{ query?: 
 ›   undefined; header?: undefined; path?: undefined; 
 ›   cookie?: undefined; }'.

Versions

{
  "devDependencies": {
    "ts-to-zod": "^3.13.0",
    "typescript": "^5.6.3",
    "zod": "^3.23.8"
  }
}
lensbart commented 3 hours ago

When I run the same command with --skipValidation, I see the following error:

    SyntaxError: Unexpected token (1702:38)
      1700 |         get: z.never().optional(),
      1701 |         put: z.never().optional(),
    > 1702 |         post: 
    operationsSchema.shape."session.alert",
           |                                      ^
      1703 |         delete: z.never().optional(),
      1704 |         options: z.never().optional(),
      1705 |         head: z.never().optional(),