`decode-formdata` is a JavaScript and TypeScript library designed to convert a flat `FormData` object back into a complex, nested JavaScript object. This process addresses the inherent data loss when `FormData` serializes all values to strings or `File` objects, and flattens nested structures. The library, currently at version 0.9.0, is actively maintained with recent updates focusing on bug fixes, security enhancements, and improved parsing capabilities like array bracket notation. It does not follow a strict release cadence but receives updates as needed. Its primary utility lies in reconstructing type information (booleans, numbers, dates) and object/array hierarchies from form field names using dot and bracket notation. This makes it particularly valuable for server-side processing in full-stack frameworks (e.g., Next.js, Remix, SvelteKit) where `FormData` is common for progressively enhanced forms, allowing developers to easily validate and type the re-hydrated data with schema libraries like Zod or Valibot.
npm install decode-formdataVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `decode` to transform a `FormData` object, populated with various data types and nested structures, back into a complex JavaScript object. It highlights specifying array paths, and type conversions for booleans, dates, files, and numbers, mimicking a common server-side form handling scenario.
Review any code that relies on the exact decoded value of empty strings, 'null', or 'undefined' from `FormData` entries. Update expectations or add explicit `transform` functions if specific previous behavior is desired.
No direct fix is needed as this is a security improvement. However, if your application exceptionally relied on `__proto__` or similar keys being present in the decoded object, you will need to adjust your logic or naming conventions.
Always use the `$` wildcard for array indices when defining paths in `arrays`, `booleans`, `dates`, `files`, or `numbers` options that target elements within nested arrays (e.g., `['images.$.file']`).
Ensure you are passing a proper `FormData` object to the `decode` function. In Node.js, if not using Node 16+ (which has global `FormData`), you might need to import a polyfill like `@remix-run/node`'s `FormData` or `node-fetch`'s `FormData`.
Carefully review the `options` object in your `decode` call. Ensure that `arrays`, `booleans`, `dates`, `files`, and `numbers` arrays contain the correct paths for all fields that should be converted from their string `FormData` representation to the desired type. Remember to use `$` for nested array paths.
Add the root path of the array to the `arrays` option in the `decode` function call. For example, if you have fields like `tags.0`, `tags.1`, ensure `tags` is in the `arrays` option: `decode(formData, { arrays: ['tags'], ... })`.No dependency data recorded yet.