Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

zodToJsonSchema: pipeStrategy 'output' #439

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

testerez
Copy link

@testerez testerez commented Feb 25, 2024

Using transform in the output zod schema works pretty well.
Unfortunately, the generated openapi document indicates the wrong type in response.

Here is what I get:

"amount": {
  "type": "integer",
  "format": "int64"
}

I've found that we could get the correct response definition by using zod pipe and pipeStrategy 'output':

.output(z.object({ amount: z
    .bigint()
    .transform((v) => v.toString())
    .pipe(z.string().regex(/^\d+$/)) }))

Then I get the right response definition:

"amount": {
  "type": "string",
  "pattern": "^\\d+$"
}

Related issue: #264

@testerez
Copy link
Author

Hey @jlalmes, any chance to have this merged?

@StefanTerdell
Copy link

I'm working on another option that would be useful for this case (callback). Maybe you could find a way to expose the zodToJsonSchema options object through the trpc-openapi API? BigInt serialization isn't really standardized with JSON after all, so a one-size-fits-all solution is unlikely

@testerez
Copy link
Author

Thanks for looking that this PR @StefanTerdell ,
The solution I'm proposing here is not specific to BigInt. It enables usage of z.pipe for any use case. And I think it's the correct way to generate the openapi schema. What the API returns is the output of the zod schema, not the input.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants