astahmer / openapi-zod-client

Generate a zodios (typescript http client with zod validation) from an OpenAPI spec (json/yaml)
openapi-zod-client.vercel.app
734 stars 82 forks source link

discriminator cannot be used when `--export-type` being applied #216

Open PeterMK85 opened 1 year ago

PeterMK85 commented 1 year ago

Describe the bug If the export-type being used and the schema is referencing another schema with the z.ZodType declaration, zod's z.discriminatedUnion wont' work.

Minimal reproduction Example

Check the metric field on Pet

Expected behavior

const MoneyMetric: z.ZodType<MoneyMetric> = z.object({
    id: z.string(),
    name: z.string(),
    value: z.number(),
    type: z.literal('MONEY'),
    details: z.object({ currency: z.string().min(3).max(3) }),
})

const Metric = z.discriminatedUnion('type', [MoneyMetric, NumberMetric])

Should not throw an error, however I think the root of the issue us that ZodType does not work with the discriminatedUnion

Additional context I think this reveals an under laying issue. const MoneyMetric: z.ZodType<MoneyMetric> = approach works nicely to eliminate the TS7056 error, but on the other hand the schemas which has the ZodType type casting no longer "real" Zod schemas, they cannot be merge/extend and it seems these cannot be part of the discriminatedUnion declaration