When the values of your form are encoded to FormData, for example to send them to a server via HTTP, some information is lost. Using this library, you can decode FormData into a JavaScript object and supplement the information that was lost during encoding.
This library is especially useful in combination with progressively enhanced forms via actions in fullstack frameworks such as Next.js, Nuxt, Remix, SvelteKit, SolidStart, and Qwik. Furthermore, you can validate and type the decoded data afterwards with a schema library like Valibot or Zod.
import { decode } from 'https://deno.land/x/decode_formdata/mod.ts';
How it works
FormData stores the names of your fields and their values. However, there is a problem. Only strings and files are accepted as values, but complex forms can contain booleans, strings and dates. This leads to the fact that the boolean value true must be mapped with "on" and false values are simply ignored. Numbers and dates are also converted to strings.
Another problem are objects and arrays, which are usually mapped using dot and bracket notation. For example, the input field <input name="todos.0.label" /> should map to the object { todos: [{ label: "" }] }. By telling decode where arrays, booleans, dates, files, and numbers are located, the function can decode your FormData back into a complex JavaScript object.
Both dot and bracket notation are supported for arrays.
Consider the following form to add a new product to an online store: