zod icon indicating copy to clipboard operation
zod copied to clipboard

Type "string" format "binary"

Open clabnet opened this issue 1 year ago • 3 comments

Hello guys, I tried to upload a file using zod validator, this must be run also on Swagger

body: {
        type: 'object',
        properties: {
          file: {
            type: 'string',
            format: 'binary',
          },
        },
      },

How to set with zod an object similar ? Thank's

clabnet avatar Dec 15 '22 17:12 clabnet

You might be able to achieve this by using a refinement.

z.string().refine((str): str is Buffer => Buffer.isBuffer(Buffer.from(str)))

I'm not used to working with binary data, but I think that would work.

Let me know.

BR.

santosmarco-caribou avatar Dec 16 '22 11:12 santosmarco-caribou

The question is whether in your instance you really need to validate the binary raw, not first process it into some representation. Because you can also check for z.instanceOf and then refine it. For example on a remix app I'm first processing multipart and produce File then can check z.instanceOf(File).refine(file => /* ... */). Substitute File with whatever data you have.

akomm avatar Dec 16 '22 13:12 akomm

@clabnet ok to mark this as resolved?

santosmarco-caribou avatar Dec 20 '22 18:12 santosmarco-caribou

Yes close it, thank's

clabnet avatar Dec 23 '22 20:12 clabnet

Hello guys I tried to do This z.string().refine((str): str is Buffer => Buffer.isBuffer(Buffer.from(str))) But it didn't work Swagger still read it as a text input rather than file picker

mohrazzak avatar Oct 13 '23 08:10 mohrazzak

@clabnet @santosmarco-caribou

mohrazzak avatar Oct 13 '23 08:10 mohrazzak