-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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 |
60b6b54
to
b2bcf10
Compare
Hi @heath-freenome, I think I did what requested. I also added an example in the playground package. |
63f6e99
to
2982d9f
Compare
@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
@@ -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)) { |
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
patternProperties
JSONSchema directive. In detail:@rjsf/core
, modified theObjectField
widget to replicate theadditionalProperties
behaviour@rjsf/utils
:PATTERN_PROPERTIES_KEY
has been addedcanExpand
returnstrue
ifpatternProperties
is present in an object schemagetSchemaType
function now returnsobject
also whenpatternProperties
is presentretrieveSchema
function has been extended to support thepatternProperties
validationCaveat
The
handleAddClick
method of theObjectField
generates a new property with given name and value. WhenadditionalProperties
is not present in the schema, this new property may not match any of thepatternProperties
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. TheonKeyChange
method then takes care of generating a correct form as soon as the key matches one or morepatternProperties
keys.