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

binary properties cause failure in server environments #225

Open
s-h-a-d-o-w opened this issue Sep 8, 2023 · 1 comment
Open

binary properties cause failure in server environments #225

s-h-a-d-o-w opened this issue Sep 8, 2023 · 1 comment

Comments

@s-h-a-d-o-w
Copy link
Contributor

s-h-a-d-o-w commented Sep 8, 2023

Describe the bug
When using the generated code in a server environment (which might obviously become common due to React server components), the following error is thrown: ReferenceError: File is not defined

Minimal reproduction
See this branch: https://github.com/s-h-a-d-o-w/openapi-zod-client-experiment/tree/file-problem-repro

The error is emitted when running the dev server.

Expected behavior
I believe the ideal solution would be to generate isomorphic code that looks like the following:

schema: z.custom<File | Buffer>((data) => {
  return typeof window === 'undefined' ? data instanceof Buffer : data instanceof File
}, 'Input is not an instance of a Buffer or File'),

Depending on the use case, the user might obviously have to assert to either File or Buffer but I think that's preferable to the code simply breaking. 😅

As you can maybe tell by looking at the rest of the app, I have already tested this approach there.

@astahmer
Copy link
Owner

astahmer commented Sep 9, 2023

at some point I also thought of just removing the File altogether as I'm not sure this is worth including or even useful for anyone

I'm fine with both

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