Skip to content

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

Merged
merged 3 commits into from
May 1, 2025

Conversation

GlassOfWhiskey
Copy link
Contributor

Reasons for making this change

Fixes #1944 (which has been closed without being resolved) by extending RJSF support to the patternProperties JSONSchema directive. In detail:

  • In @rjsf/core, modified the ObjectField widget to replicate the additionalProperties behaviour
  • In @rjsf/utils:
    • A new PATTERN_PROPERTIES_KEY has been added
    • Now canExpand returns true if patternProperties is present in an object schema
    • The getSchemaType function now returns object also when patternProperties is present
    • The retrieveSchema function has been extended to support the patternProperties validation

Caveat

The handleAddClick method of the ObjectField generates a new property with given name and value. When additionalProperties is not present in the schema, this new property may not match any of the patternProperties keys, 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. The onKeyChange method then takes care of generating a correct form as soon as the key matches one or more patternProperties keys.

@heath-freenome
Copy link
Member

heath-freenome commented Apr 29, 2025

@GlassOfWhiskey thanks for the bug fix. With v5 being locked down for anything but emergency fixes, Can you rebase this onto the rjsf-v6 branch? You could also wait about 5 days and that branch will be merged down to mainline for the v6 beta program. Also, can you update the docs to indicate the support for this?

@GlassOfWhiskey GlassOfWhiskey changed the base branch from main to rjsf-v6 April 30, 2025 13:19
@GlassOfWhiskey GlassOfWhiskey force-pushed the main branch 5 times, most recently from 60b6b54 to b2bcf10 Compare April 30, 2025 14:04
@GlassOfWhiskey
Copy link
Contributor Author

Hi @heath-freenome, I think I did what requested. I also added an example in the playground package.

@GlassOfWhiskey GlassOfWhiskey force-pushed the main branch 2 times, most recently from 63f6e99 to 2982d9f Compare April 30, 2025 17:00
@heath-freenome
Copy link
Member

@GlassOfWhiskey can you run npm run cs-format to fix the linting errors?

@GlassOfWhiskey
Copy link
Contributor Author

@GlassOfWhiskey can you run npm run cs-format to fix the linting errors?

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.

@heath-freenome
Copy link
Member

@GlassOfWhiskey can you run npm run cs-format to fix the linting errors?

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) {
Copy link
Member

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) {
Copy link
Member

Choose a reason for hiding this comment

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

Does !isEmpty(patternProperties) work?

Comment on lines 400 to 408
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']>,
);
Copy link
Member

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

Comment on lines 551 to 559
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']>,
);
Copy link
Member

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

@@ -195,36 +195,39 @@ class ObjectField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
* @param schema - The schema element to which the new property is being added
*/
handleAddClick = (schema: S) => () => {
if (!schema.additionalProperties) {
if (!(schema.patternProperties || schema.additionalProperties)) {
Copy link
Member

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

Suggested change
if (!(schema.patternProperties || schema.additionalProperties)) {
if (!(schema.additionalProperties || schema.patternProperties)) {

@heath-freenome heath-freenome merged commit d2057ce into rjsf-team:rjsf-v6 May 1, 2025
2 of 4 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.

Support for patternProperties
3 participants