Skip to content

formToJSON should not separate properties with minus character #5402

Description

@gkurt

Describe the bug

formToJSON is a helpful function and I use it when I have a form to submit, but do additional validation on individual properties before sending it to backend. However, when I have a form input with a name like user-name, this property is converted to object as { user: { name: '' } } instead of { 'user-name': '' }. I think this is an unexpected behavior.

To Reproduce

var axios = require("axios")
var { JSDOM  } = require("jsdom")
var { FormData } = new JSDOM().window;

const formData = new FormData();
formData.append('user-name', 'johndoe');

const result = axios.formToJSON(formData);
console.log(result);

Code snippet

Try the above snippet.

Expected behavior

Properties should only be splitted with . and [ ]. Currently, they are splitted by - and (space) too.

While it can be argued that . and [ ] can also be inside a property key, they are less likely than - and . As an advanced option, there can be an escape character to allow this case. However this may be an overkill.

Axios Version

1.2.1

Adapter Version

Browser

All browsers

Browser Version

All versions

Node.js Version

All versions

OS

All OS

Additional Library Versions

Additional context/Screenshots

Metadata

Metadata

Assignees

Labels

issue::bugThis issue is related to a bug that requires fixing

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions