#Validating MultipartFormData / a form containing a File

1 messages · Page 1 of 1 (latest)

proud tundraBOT
#

Hi @proven shuttle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Nuxt/Nitro don’t currently document an “official” best‑practice pattern for validating FormData/MultipartFormData with File objects on the server, and the knowledge sources here only show that Nuxt recently added specific handling for File inside FormData in core. [handle File FormData]

What we can infer from that:

  • Nuxt/Nitro are moving towards correctly preserving File instances when FormData is sent from the client, instead of having everything turned into raw buffers. [handle File FormData]
  • However, there is no documented, higher‑level validation helper for multipart bodies in the provided sources.

Given the lack of explicit guidance, you essentially have two options:

  1. Manual validation after parsing

    • Use the Nitro helpers (such as readBody / multipart helpers) to get the FormData/fields.
    • For non‑file fields, validate them as usual with your schema library of choice (e.g. by building a plain object from the fields and passing it to your validator).
    • For files, validate manually:
      • check existence (required / optional),
      • check MIME type,
      • check file size,
      • optionally inspect filename/extension.

    This approach still means some coercion/normalisation step (turning multipart fields into a validation‑friendly object), and there’s no indication in the docs that Nitro abstracts this away today.

  2. Custom abstraction around multipart parsing

#
  • Build your own small helper that:
    • calls the Nitro multipart reader,
    • converts the non‑file fields into plain strings/numbers/booleans,
    • keeps files as File (or wraps Buffers with metadata),
    • returns an object shaped for your validator ({ fields, files }).
    • You then validate fields with a schema library and files with your own rules.

Because the Nuxt change you linked only deals with ensuring File inside FormData is handled correctly at a low level and does not add a validation API, there’s no single recommended pattern in the knowledge sources beyond these generic approaches. [handle File FormData]

So: you can rely on Nuxt now properly preserving File within FormData, but you still need to implement your own validation layer on top; the knowledge sources do not describe a built‑in or canonical “best practice” for multipart validation.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: