Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I specify the format? #95

Closed
thelinuxlich opened this issue Dec 7, 2023 · 7 comments
Closed

How can I specify the format? #95

thelinuxlich opened this issue Dec 7, 2023 · 7 comments

Comments

@thelinuxlich
Copy link

I want to specify a file upload format like described in https://swagger.io/docs/specification/describing-request-body/file-upload/ which needs to be binary. Is it possible to do this?

@StefanTerdell
Copy link
Owner

From what I can tell there's no native way to do that in Zod. You can open an issue or a PR in the Zod repo to add something like z.string().base64(). Then we can represent that as format: "binary" if the target is Open API.

@thelinuxlich
Copy link
Author

I'd convert these to binary:

z.instanceof(File)
z.instanceof(Buffer)
z.instanceof(Blob)

@StefanTerdell
Copy link
Owner

There's no way to serialize an instanceof check unfortunately, and you're not describing the serialized data with that schema anyway. The base64 route is probably your best bet still

@StefanTerdell
Copy link
Owner

Added a PR over at Zod for a base64 check. I guess we'll see how that goes :) colinhacks/zod#3047

@StefanTerdell
Copy link
Owner

Hello! A solution to this is now available as of 3.23.0. Since the schema is interpreted to represent the serialized data you can use z.string().base64() to represent file contents. By default this results in "contentEncoding": "base64" as described here but there's also an option ({ "base64Strategy": "format:binary" }) to set it to the OpenAPI <=3.0 equivalent

@StefanTerdell
Copy link
Owner

@thelinuxlich

@thelinuxlich
Copy link
Author

Hey Stefan, this is great! Finally we can represent this type with Zod!

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

No branches or pull requests

2 participants