-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Added support for patternProperties
#4582
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
Conversation
|
@GlassOfWhiskey thanks for the bug fix. With v5 being locked down for anything but emergency fixes, Can you rebase this onto the |
|
Hi @heath-freenome, I think I did what requested. I also added an example in the playground package. |
|
@GlassOfWhiskey can you run |
Sorry I didn't notice that lint dependencies were changed in v6. I reran the formatter, but I only pushed the changes to the files I actually modified. There were other changes in the components package, but I left them for another PR if needed. |
I believe that my PR #4583 gets those |
| }, | ||
| {} as NonNullable<S['patternProperties']>, | ||
| ); | ||
| if (Object.keys(patternProperties).length > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does !isEmpty(patternProperties) work?
| }, | ||
| {} as NonNullable<S['patternProperties']>, | ||
| ); | ||
| if (Object.keys(patternProperties).length > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does !isEmpty(patternProperties) work?
| const patternProperties = Object.keys(schema.patternProperties!) | ||
| .filter((pattern) => RegExp(pattern).test(key)) | ||
| .reduce( | ||
| (obj, pattern) => { | ||
| obj[pattern] = schema.patternProperties![pattern]; | ||
| return obj; | ||
| }, | ||
| {} as NonNullable<S['patternProperties']>, | ||
| ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a duplicate of the code at lines 551-559, consider drying things up with a helper method
| const patternProperties = Object.keys(schema.patternProperties!) | ||
| .filter((pattern) => RegExp(pattern).test(key)) | ||
| .reduce( | ||
| (obj, pattern) => { | ||
| obj[pattern] = schema.patternProperties![pattern]; | ||
| return obj; | ||
| }, | ||
| {} as NonNullable<S['patternProperties']>, | ||
| ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a duplicate of the code at lines 400-408, consider drying things up with a helper method
| */ | ||
| handleAddClick = (schema: S) => () => { | ||
| if (!schema.additionalProperties) { | ||
| if (!(schema.patternProperties || schema.additionalProperties)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This matches the order in code down in utils
| if (!(schema.patternProperties || schema.additionalProperties)) { | |
| if (!(schema.additionalProperties || schema.patternProperties)) { |
Reasons for making this change
Fixes #1944 (which has been closed without being resolved) by extending RJSF support to the
patternPropertiesJSONSchema directive. In detail:@rjsf/core, modified theObjectFieldwidget to replicate theadditionalPropertiesbehaviour@rjsf/utils:PATTERN_PROPERTIES_KEYhas been addedcanExpandreturnstrueifpatternPropertiesis present in an object schemagetSchemaTypefunction now returnsobjectalso whenpatternPropertiesis presentretrieveSchemafunction has been extended to support thepatternPropertiesvalidationCaveat
The
handleAddClickmethod of theObjectFieldgenerates a new property with given name and value. WhenadditionalPropertiesis not present in the schema, this new property may not match any of thepatternPropertieskeys, resulting in an invalid property.In this case, a new property with
{ type: 'null' }is generated in order to let the user visualize the key form field and modify the content. TheonKeyChangemethod then takes care of generating a correct form as soon as the key matches one or morepatternPropertieskeys.