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

Bug: Issue with array schema defaults not applying properly when formData is an empty array #4359

Merged
merged 5 commits into from
Nov 8, 2024

Conversation

abdalla-rko
Copy link
Contributor

@abdalla-rko abdalla-rko commented Oct 31, 2024

Reasons for making this change

Fixes #4335

when you enable the mergeExtraDefaults and you have formData as an empty array, the default on the root of the array schema doesn't get applied and only applies the default from the items property. If formData is undefined or null, it will apply the default from the root schema and items.default and the root default will take precedence over item property default.

Checklist

  • I'm adding or updating code
    • I've added and/or updated tests. I've run npx nx run-many --target=build --exclude=@rjsf/docs && npm run test:update to update snapshots, if needed.
    • I've updated docs if needed
    • I've updated the changelog with a

Comment on lines 435 to 437
const arrayMinItemsStateBehavior = experimental_defaultFormStateBehavior?.arrayMinItems;
const arrayMinItemsPopulate = arrayMinItemsStateBehavior?.populate;
const arrayMergeExtraDefaults = arrayMinItemsStateBehavior?.mergeExtraDefaults;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider doing this?:

Suggested change
const arrayMinItemsStateBehavior = experimental_defaultFormStateBehavior?.arrayMinItems;
const arrayMinItemsPopulate = arrayMinItemsStateBehavior?.populate;
const arrayMergeExtraDefaults = arrayMinItemsStateBehavior?.mergeExtraDefaults;
const { arrayMinItems: arrayMinItemsStateBehavior = {} } = experimental_defaultFormStateBehavior;
const { populate: arrayMinItemsPopulate, mergeExtraDefaults: arrayMergeExtraDefaults } = arrayMinItemsStateBehavior;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not possible to destructure experimental_defaultFormStateBehavior since it can be undefined but I improved it a little.

@heath-freenome
Copy link
Member

Also, would you be willing to change the example for these docs to switch arrayMinItems to be emptyObjectFields?

@heath-freenome
Copy link
Member

@abdalla-rko Have you had the change to consider my feedback/request yet?

@heath-freenome heath-freenome merged commit 62f3397 into rjsf-team:main Nov 8, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Array defaults don't work at the root of the schema.
2 participants