Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

formData includes undefined properties #3424

Closed
4 tasks done
AlexMunoz opened this issue Feb 1, 2023 · 2 comments · Fixed by #3430
Closed
4 tasks done

formData includes undefined properties #3424

AlexMunoz opened this issue Feb 1, 2023 · 2 comments · Fixed by #3430
Labels

Comments

@AlexMunoz
Copy link

AlexMunoz commented Feb 1, 2023

Prerequisites

What theme are you using?

core

Version

5.0.1

Current Behavior

Hello, I am migrating from a beta version to the release version of RJSF and I noticed that after beta.19 the formData object in the onChange and Submit event includes some undefined properties.

In version beta.19, the formData looks like:

{title: "A new task", done: false}

and after beta.19, the formData looks like:

{title: "A new task", name: undefined, done: false}

Expected Behavior

Only the fields I have changed are included in the formData.

Steps To Reproduce

  1. Visit the samples.
  2. Go to the console Tab.
  3. Check the logs

Version 5.0.0-beta.19
https://codesandbox.io/s/rjsf-5-0-0-beta-19-ff6foc?file=/src/App.js

Version 5.0.1
https://codesandbox.io/s/rjsf-5-0-1-qhq3p6?file=/src/App.js

Environment

- OS: Linux
- Node: 16.15
- npm: 8.11

Anything else?

I am not sure if this behavior is expected. But it only occurs for properties at the first level, nested properties are not included as undefined.

why I need this: well I am doing a deep comparison between the current formData with the initial one, to detect if changes were made. If there is another way to check this, please let me know.

Update:
I think the change was introduced here: #3408

const formData: T = schemaUtils.getDefaultFormState(
schema,
inputFormData,
"excludeObjectChildren"
) as T;

@AlexMunoz AlexMunoz added bug needs triage Initial label given, to be assigned correct labels and assigned labels Feb 1, 2023
@AlexMunoz AlexMunoz changed the title formData with undefined properties formData includes undefined properties Feb 1, 2023
@heath-freenome
Copy link
Member

heath-freenome commented Feb 1, 2023

@AlexMunoz What happens if you use liveOmit? Also, how are you doing a deep comparison? Theoretically { x: undefined } is the same as {} depending on the way you compare.

@heath-freenome heath-freenome removed the needs triage Initial label given, to be assigned correct labels and assigned label Feb 1, 2023
@AlexMunoz
Copy link
Author

AlexMunoz commented Feb 2, 2023

Hi @heath-freenome
I created a new sample https://codesandbox.io/s/rjsf-5-0-1-liveomit-2xd11r?file=/src/App.js and liveOmit ist not working for this case.

I think, liveOmit works in the case that the initial dataForm has an attribute that it not present in the schema.

For example, if we have:

initial formData
 const formData = { 
   title: "myTitle",
   asdf: 1
 }
 
const schema = {
  title: "Todo",
  type: "object",
  required: ["title"],
  properties: {
    title: { type: "string", title: "Title"},
    name: { type: "string", title: "Name"},
  }
};
 

liveOmit will remove asfd because is not defined in the schema.

{ 
  title: "myTitle"
}

The problem that I've with RJSF after the 5.0.0-beta.19 version is that my formData is different after the initial mount.

initial formData
 const formData = { 
   title: "myTitle",
 }
 
const schema = {
  title: "Todo",
  type: "object",
  required: ["title"],
  properties: {
    title: { type: "string", title: "Title"},
    name: { type: "string", title: "Name"},
  }
};

After mount the formData will be:

formData = { 
   title: "myTitle",
   name: undefined
 }

Theoretically:{ x: undefined } is the same as {}, but a deep comparison using lodash.isEqualWith or deequal will fail
because { title: 'Title 1'} and { title: 'Title 1', age: undefined} do not have the same properties.

I've also created a sample here: https://codesandbox.io/s/compare-objects-x3kwj4

heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Feb 4, 2023
Fixes: rjsf-team#3424 by preventing the inclusion of undefined properties at the root level
- Updated `@rjsf/utils`, making `computDefaults` helper in `getDefaultFormState()` to skip adding undefined values when `excludeObjectChildren` is set.
  - This basically supports adding an empty object
  - Updated the tests accordingly
- Updated the `CHANGELOG.md` accordingly
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Feb 4, 2023
Fixes: rjsf-team#3424 by preventing the inclusion of undefined properties at the root level
- Updated `@rjsf/utils`, making `computeDefaults` helper in `getDefaultFormState()` to skip adding undefined values when `excludeObjectChildren` is set.
  - This basically supports adding an empty object
  - Updated the tests accordingly
- Updated the `CHANGELOG.md` accordingly
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Feb 4, 2023
Fixes: rjsf-team#3424 by preventing the inclusion of undefined properties at the root level
- Updated `@rjsf/utils`, making `computeDefaults` helper in `getDefaultFormState()` to skip adding undefined values when `excludeObjectChildren` is set.
  - This basically supports adding an empty object
  - Updated the tests accordingly
- Updated the `CHANGELOG.md` accordingly
  - Also added information the changelog for PR rjsf-team#3202
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Feb 4, 2023
Fixes: rjsf-team#3424 by preventing the inclusion of undefined properties at the root level
- Updated `@rjsf/utils`, making `computeDefaults()` helper in `getDefaultFormState()` to skip adding undefined values when `allowEmptyObject` is set.
  - Updated the `getDefaultFormState()` definition in the `ValidatorType` to add this new option
  - Updated the tests accordingly
- Updated `@rjsf/core`, switching the `excludeObjectChildren` with `allowEmptyObject` in `Form`
- Updated the documentation for `getDefaultFormState()` to add this new option
- Updated the `CHANGELOG.md` accordingly
  - Also added information the changelog for PR rjsf-team#3202
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Feb 5, 2023
Fixes: rjsf-team#3424 by preventing the inclusion of undefined properties at the root level
- Updated `@rjsf/utils`, making `computeDefaults()` helper in `getDefaultFormState()` to skip adding undefined values when `allowEmptyObject` is set.
  - Updated the `getDefaultFormState()` definition in the `ValidatorType` to add this new option
  - Updated the tests accordingly
- Updated `@rjsf/core`, switching the `excludeObjectChildren` with `allowEmptyObject` in `Form`
- Updated the documentation for `getDefaultFormState()` to add this new option
- Updated the `CHANGELOG.md` accordingly
  - Also added information the changelog for PR rjsf-team#3202
heath-freenome added a commit that referenced this issue Feb 5, 2023
* Fix: Prevent undefined properties at the root level
Fixes: #3424 by preventing the inclusion of undefined properties at the root level
- Updated `@rjsf/utils`, making `computeDefaults()` helper in `getDefaultFormState()` to skip adding undefined values when `allowEmptyObject` is set.
  - Updated the `getDefaultFormState()` definition in the `ValidatorType` to add this new option
  - Updated the tests accordingly
- Updated `@rjsf/core`, switching the `excludeObjectChildren` with `allowEmptyObject` in `Form`
- Updated the documentation for `getDefaultFormState()` to add this new option
- Updated the `CHANGELOG.md` accordingly
  - Also added information the changelog for PR #3202

* - Responded to reviewer feedback
@nickgros nickgros mentioned this issue Feb 10, 2023
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants