StefanTerdell / zod-to-json-schema

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

z.pipe can create impossible validation schemas #97

Closed ciscoheat closed 9 months ago

ciscoheat commented 9 months ago

I noticed that z.pipe can create conflicting schemas due to the allOf inclusion of the types. A crude example:

const schema = z.object({
  len: z.string().transform((val) => val.length).pipe(z.number().min(5))
});
{
  type: 'object',
  properties: {
    // Can never validate, conflicting types
    len: { allOf: [ { type: 'string' }, { type: 'number', minimum: 5 } ] }
  },
  required: [ 'len' ],
  additionalProperties: false,
  '$schema': 'http://json-schema.org/draft-07/schema#'
}

I saw that pipeStrategy can be used to only get the input type, so would you be open to an output option for that, if possible? If it's relatively simple, I'll make a PR for it.

StefanTerdell commented 9 months ago

That's the thing, short of using a compiler you can't get the output type to serialize it. But I guess an option to pick the last schema in the pipe could work.

StefanTerdell commented 9 months ago

🚀 3.22.2

ciscoheat commented 9 months ago

Thank you for a very quick merge! :)