#Validating MultipartFormData / a form containing a File
1 messages · Page 1 of 1 (latest)
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
Fileinstances whenFormDatais 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:
-
Manual validation after parsing
- Use the Nitro helpers (such as
readBody/ multipart helpers) to get theFormData/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.
- Use the Nitro helpers (such as
-
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
fieldswith a schema library andfileswith 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]