fabian-hiller / decode-formdata Goto Github PK
View Code? Open in Web Editor NEWDecodes complex FormData into a JavaScript object
License: MIT License
Decodes complex FormData into a JavaScript object
License: MIT License
What do you thing about a new notation for FormDataInfo? Something that looks more like the object that will get returned:
const formValues = decode(formData, {
title: 'string',
price: 'number',
created: 'date',
active: 'boolean',
tags: ['string'],
images: [{
title: 'string',
created: 'date',
file: 'file',
}]
,
});
Benefits:
One issue I ran into was that the decode function doesn't parse "true"
or "false"
strings to their corresponding boolean values. Both just result in true
. To get false
, you need to exclude the field from the form data altogether.
I think this could be an area for improvement.
"false"
, "0"
"false"
, "0"
, excludedHey!
I just noticed that handling multiple entries with the same name will result in a single prop with the last value. Coming from plain ol HTML forms, I would expect that they turn into an array.
This situation occurs when I have multiple checkboxes with the same name, or (more problematic) a multi select:
<form id="myForm">
<input type="checkbox" name="pets" value="cat" />
<input type="checkbox" name="pets" value="dog" />
<input type="checkbox" name="pets" value="pig" />
<select name="colors" multiple>
<option>Blue</option>
<option>Red</option>
<option>Gold</option>
</select>
</form>
const formData = new FormData($('#myForm'));
Array.from(formData);
// [
// ['pets', 'cat']
// ['pets', 'dog']
// ['colors', 'Red']
// ['colors', 'Gold']
// ]
decode(formData, { arrays: ['pets', 'colors'] })
// {
// pets: 'dog',
// colors: 'Gold'
// }
I would expect to receive an array, if the same key is found multiple times, regardless if it's defined in the FormDataInfo
or not. But the info need to be configured properly, to enforce receiving an array, if only a single value is selected, as we can not differentiate it from single value entries.
What do you think?
Found one more issue related to dates. If a user leaves a date field empty, it returns as ""
and the decode function will return Invalid Date
. This will cause a schema to fail, even if it accepts empty dates.
I currently handle it like this:
const formValues = decode(formData, {
...info,
dates: info?.dates?.filter((d) => formData.get(d)) // filter empty strings
});
This ensures they show up as ""
after decoding.
Formdata content is always stored as a string. So if I am sending a Timestamp to the server it will be converted to a string and fail.
const formData = new FormData()
formData.append('createdDate', new Date().getTime())
const data = decode(formData, { dates:['createdDate'] })
console.log(data)
{
createdDate: 2020-02-20T00:00:00.000Z
}
{
createdDate: Invalid Date
}
btw, your package is awesome.
I think that we can fully type the value paths of the seconds argument of the decode
function using a generic. This way, we can make the API fully type safe. All we need to do is resolve the path of each data type in TypeScript. I have already implemented this in a similar way for Modular Forms.
Furthermore, my idea is that if a generic is specified, for example decode<MyType>(...)
, the return type of decode
is still Record<string, unknown>
, and this behavior can be controlled by a second generic, for example a boolean. So decode<MyType, true>(...)
would have the return type MyType
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.