Skip to content

Validator: FormData validation fails when content-type header does not have a space #3285

@Ernxst

Description

@Ernxst

What version of Hono are you using?

4.5.5

What runtime/platform is your app running on?

Cloudflare Workers

What steps can reproduce the bug?

  1. Define a route with a validator schema (I'm using the example in the docs)
const route = app.post(
  '/posts',
  zValidator(
    'form',
    z.object({
      body: z.string(),
    })
  ),
  (c) => {
    const validated = c.req.valid('form')
    // ... use your validated data
  }
)
  1. Call endpoint with form data matching expecting shape.

What is the expected behavior?

c.req.valid('form') to parse the form data.

What do you see instead?

A validation error is thrown. Adding logs inside validator.js (in my node_modules) shows that the validator function is receiving an empty object. It seems that the multipart regex test fails so an empty object is returned. In my case, it fails because the Content-Type header does not have a space after the semi-colon (multipart/form-data;boundary=2b6246d41cda0544685d0b416bddc327) like the regex is expecting.

Additional information

This used to work fine, not clue what it is in my stack that has removed the space in the Content-Type header.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions