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

Add the ability to parse defaults from allOf schema #3969

Merged
merged 4 commits into from
Nov 30, 2023

Conversation

benjdlambert
Copy link
Contributor

@benjdlambert benjdlambert commented Nov 20, 2023

Reasons for making this change

Fixes #3892.

This adds an opt in config option to be able to derive the default values from allOf schema including the if-then-else syntax. Followed the implementation options listed in the PR. Maybe it would make sense to follow the same config types as arrayMinItems and have allOf.populate as the defaultFormOptions, but will leave that up to the reviewer.

Checklist

  • I'm updating documentation
  • I'm adding or updating code
    • I've added and/or updated tests. I've run npm run test:update to update snapshots, if needed.
    • I've updated docs if needed
    • I've updated the changelog with a description of the PR
  • I'm adding a new feature
    • I've updated the playground with an example use of the feature

experimental_defaultFormStateBehavior?.allOf === 'populateDefaults' && ALL_OF_KEY in schema
? retrieveSchema<T, S, F>(validator, schema, rootSchema, formData)
: schema;
const objectDefaults = Object.keys(retrievedSchema.properties || {}).reduce(
Copy link
Member

Choose a reason for hiding this comment

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

I forget if it is possible for allOf to be in something other than the object type?

Copy link
Member

Choose a reason for hiding this comment

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

@benjdlambert Do you know whether this is possible? I can imagine if it is, then this solution may be incomplete

Copy link
Contributor Author

@benjdlambert benjdlambert Nov 22, 2023

Choose a reason for hiding this comment

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

Hmm, so I guess you mean something like this right?

const schema = { anyOf: [{ type: 'string' }, { type: 'number' }] }

Or do you have an example schema in mind that I can test with?

Copy link
Member

Choose a reason for hiding this comment

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

That's an anyOf. I'm guessing that you probably haven't seen an JSON Schema that has an allOf that isn't part of an object type?

Copy link
Member

Choose a reason for hiding this comment

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

Anyway, this is better than what we had before, so I'm going to approve

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, actually i'm not sure. I've never seen one, not sure if there's any other datatypes where it would be possible to use allOf like that. Happy to provide a followup if there are!

@heath-freenome
Copy link
Member

Can you rebase and update the CHANGELOG.md file?

@benjdlambert benjdlambert changed the title Add the ability to parse defaults from anyOf schema Add the ability to parse defaults from allOf schema Nov 22, 2023
@benjdlambert
Copy link
Contributor Author

@heath-freenome updated 🎉

CHANGELOG.md Outdated
Comment on lines 21 to 43
## @rjsf/utils

- Added an experimental flag `allOf` to `experimental_defaultFormStateBehavior` for populating defaults when using `allOf` schemas [#3969](https://github.com/rjsf-team/react-jsonschema-form/pull/3969)

Copy link
Member

Choose a reason for hiding this comment

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

Can you move this after @rjsf/fluentui-rc since we like to keep the packages in alphabetical order. And there are also conflicts due to another merged PR. Finally, it seems like you did a major reformat of the file (your IDE?). Can you revert all of those changes to previous versions?

Copy link
Contributor Author

@benjdlambert benjdlambert Nov 22, 2023

Choose a reason for hiding this comment

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

Woops, yep I thought I had saved without formatting for that file. Updated and made the changes 🎉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@heath-freenome not sure about the build failures here, seemed to have rebased but they look unrelated?

Copy link
Member

Choose a reason for hiding this comment

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

@zxbodya Could this be related to your recent PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

it indeed looks to be because of project references misconfiguration -- added fix here #3982

Copy link
Member

@heath-freenome heath-freenome left a comment

Choose a reason for hiding this comment

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

@benjdlambert I just realized that you are adding a new feature here. I just released 5.14.3, so can you make your changes be in version 5.15.0 instead?

CHANGELOG.md Outdated
- update tsconfigs:
- `"importHelpers": false` to remove need for tslib dependency [#3958](https://github.com/rjsf-team/react-jsonschema-form/issues/3958)
- increase compilation target level from es6 to es2018 (so there are no need for transpiling object spread/rest feature)
- add missing typescript project reference for `snapshot-tests` in a root tsconfig, update it to also use es modules
- Added a dropdown for chaning the `experimental_defaultFormStateBehavior.allOf` behaviour in the playground
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
- Added a dropdown for chaning the `experimental_defaultFormStateBehavior.allOf` behaviour in the playground
- Added a dropdown for changing the `experimental_defaultFormStateBehavior.allOf` behaviour in the playground

@heath-freenome
Copy link
Member

@benjdlambert sorry for the extra work... Can you resolve CHANGELOG.md conflicts again, and this time pull in the 5.14.4 changes under the 5.15.0 release since I plan to cut that after we merge your changes

@benjdlambert
Copy link
Contributor Author

@heath-freenome no worries, done! 🎉

@heath-freenome heath-freenome merged commit d89367e into rjsf-team:main Nov 30, 2023
4 checks passed
Rozamo pushed a commit to Rozamo/react-jsonschema-form that referenced this pull request Dec 10, 2023
* feat: support default values in anyOf format

* feat: add `skipDefaults` and `populateDefaults` options for `allOf` parsing

* docs: add examples

* chore: ammending chanelog
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.

Default values not set inside allOf
3 participants