Skip to content

Remove max validation when format is binary#339

Open
darkbasic wants to merge 2 commits into
astahmer:mainfrom
darkbasic:feat/binary-max
Open

Remove max validation when format is binary#339
darkbasic wants to merge 2 commits into
astahmer:mainfrom
darkbasic:feat/binary-max

Conversation

@darkbasic

Copy link
Copy Markdown
{
  type: 'string',
  format: 'binary',
  contentMediaType: 'application/octet-stream',
  maxLength: 20480
}
 const StoreChecklistTemplateRequest = z
-  .object({ description: z.string().max(255), file: z.instanceof(File).max(10240) })
+  .object({ description: z.string().max(255), file: z.instanceof(File) })
   .passthrough()

When there is a maxLength prop openapi-zod-client automatically adds the max operator to ensure the number of characters is appropriate. Unfortunately this won't generate valid zod code whenever the format is of type binary because max is not a valid method for File (Property 'max' does not exist on type 'ZodType<File, ZodTypeDef, File>').

This patch conditionally removes max for binary formats.

I thought about changing it with something like the following:

`.refine(file => file.size <= ${schema.maxLength} * 1024, {
  message: "File must be smaller than ${schema.maxLength}KB",
})`

but I'm not sure whether the openapi specs means bytes, KB, characters or what else whenever you specify maxLength in a binary format.

@vercel

vercel Bot commented Jul 11, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
openapi-zod-client-rim4 Ready Ready Preview Comment Sep 24, 2025 5:37pm

@darkbasic

Copy link
Copy Markdown
Author

I've added a regression test to prevent future occurrences.

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

Successfully merging this pull request may close these issues.

1 participant