Skip to content

Commit 573980d

Browse files
Fix form data propagation with patternProperties (#4617)
1 parent fdc3b26 commit 573980d

File tree

4 files changed

+102
-6
lines changed

4 files changed

+102
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ should change the heading of the (upcoming) version to include a major version b
1616
1717
-->
1818

19+
# 6.0.0-beta.8
20+
21+
## @rjsf/util
22+
23+
- Fixed form data propagation with `patternProperties` [#4617](https://github.com/rjsf-team/react-jsonschema-form/pull/4617)
24+
1925
# 6.0.0-beta.7
2026

2127
## @rjsf/core

packages/utils/src/findSchemaDefinition.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { GenericObjectType, RJSFSchema, StrictRJSFSchema } from './types';
66
import isObject from 'lodash/isObject';
77
import isEmpty from 'lodash/isEmpty';
88
import UriResolver from 'fast-uri';
9+
import get from 'lodash/get';
910

1011
/** Looks for the `$id` pointed by `ref` in the schema definitions embedded in
1112
* a JSON Schema bundle
@@ -51,15 +52,15 @@ export function splitKeyElementFromObject(key: string, object: GenericObjectType
5152
* @param $ref - The ref string for which the schema definition is desired
5253
* @param [rootSchema={}] - The root schema in which to search for the definition
5354
* @param recurseList - List of $refs already resolved to prevent recursion
54-
* @param baseURI - The base URI to be used for resolving relative references
55+
* @param [baseURI=rootSchema['$id']] - The base URI to be used for resolving relative references
5556
* @returns - The sub-schema within the `rootSchema` which matches the `$ref` if it exists
5657
* @throws - Error indicating that no schema for that reference could be resolved
5758
*/
5859
export function findSchemaDefinitionRecursive<S extends StrictRJSFSchema = RJSFSchema>(
5960
$ref?: string,
6061
rootSchema: S = {} as S,
6162
recurseList: string[] = [],
62-
baseURI: string | undefined = ID_KEY in rootSchema ? rootSchema[ID_KEY] : undefined,
63+
baseURI: string | undefined = get(rootSchema, [ID_KEY]),
6364
): S {
6465
const ref = $ref || '';
6566
let current: S | undefined = undefined;
@@ -123,7 +124,7 @@ export function findSchemaDefinitionRecursive<S extends StrictRJSFSchema = RJSFS
123124
export default function findSchemaDefinition<S extends StrictRJSFSchema = RJSFSchema>(
124125
$ref?: string,
125126
rootSchema: S = {} as S,
126-
baseURI: string | undefined = ID_KEY in rootSchema ? rootSchema[ID_KEY] : undefined,
127+
baseURI: string | undefined = get(rootSchema, [ID_KEY]),
127128
): S {
128129
const recurseList: string[] = [];
129130
return findSchemaDefinitionRecursive($ref, rootSchema, recurseList, baseURI);

packages/utils/src/schema/retrieveSchema.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ export function resolveReference<T = any, S extends StrictRJSFSchema = RJSFSchem
334334
* @param schema - The schema for which resolving all references is desired
335335
* @param rootSchema - The root schema that will be forwarded to all the APIs
336336
* @param recurseList - List of $refs already resolved to prevent recursion
337-
* @param baseURI - The base URI to be used for resolving relative references
337+
* @param [baseURI] - The base URI to be used for resolving relative references
338338
* @returns - given schema will all references resolved or the original schema if no internal `$refs` were resolved
339339
*/
340340
export function resolveAllReferences<S extends StrictRJSFSchema = RJSFSchema>(
@@ -432,7 +432,7 @@ export function stubExistingAdditionalProperties<
432432
validator,
433433
{ allOf: Object.values(matchingProperties) } as S,
434434
rootSchema,
435-
formData as T,
435+
get(formData, [key]) as T,
436436
experimental_customMergeAllOf,
437437
);
438438
set(schema.properties, [key, ADDITIONAL_PROPERTY_FLAG], true);
@@ -578,7 +578,7 @@ export function retrieveSchemaInternal<
578578
validator,
579579
{ allOf: [schema.properties[key], ...Object.values(matchingProperties)] } as S,
580580
rootSchema,
581-
rawFormData as T,
581+
get(rawFormData, [key]) as T,
582582
experimental_customMergeAllOf,
583583
);
584584
}

packages/utils/test/schema/retrieveSchemaTest.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,95 @@ export default function retrieveSchemaTest(testValidator: TestValidatorType) {
13381338
});
13391339
});
13401340
describe('withPatternProperties()', () => {
1341+
it('validates schemas with conditions inside patternProperties', () => {
1342+
const schema: RJSFSchema = {
1343+
type: 'object',
1344+
properties: {
1345+
foo: {
1346+
type: 'object',
1347+
},
1348+
},
1349+
patternProperties: {
1350+
'^[a-z]+$': {
1351+
type: 'object',
1352+
properties: {
1353+
isString: { type: 'boolean' },
1354+
},
1355+
allOf: [
1356+
{
1357+
if: {
1358+
properties: { isString: { const: true } },
1359+
},
1360+
then: {
1361+
properties: {
1362+
value: {
1363+
type: 'string',
1364+
},
1365+
},
1366+
},
1367+
},
1368+
{
1369+
if: {
1370+
properties: { isString: { const: false } },
1371+
},
1372+
then: {
1373+
properties: {
1374+
value: {
1375+
type: 'number',
1376+
},
1377+
},
1378+
},
1379+
},
1380+
],
1381+
},
1382+
},
1383+
};
1384+
const rootSchema: RJSFSchema = { definitions: {} };
1385+
testValidator.setReturnValues({
1386+
isValid: [true, false, true, false],
1387+
});
1388+
expect(
1389+
retrieveSchema(testValidator, schema, rootSchema, {
1390+
foo: { isString: true },
1391+
bar: { isString: true },
1392+
}),
1393+
).toEqual({
1394+
...schema,
1395+
properties: {
1396+
foo: {
1397+
type: 'object',
1398+
properties: { isString: { type: 'boolean' }, value: { type: 'string' } },
1399+
},
1400+
bar: {
1401+
[ADDITIONAL_PROPERTY_FLAG]: true,
1402+
type: 'object',
1403+
properties: { isString: { type: 'boolean' }, value: { type: 'string' } },
1404+
},
1405+
},
1406+
});
1407+
testValidator.setReturnValues({
1408+
isValid: [false, true, false, true],
1409+
});
1410+
expect(
1411+
retrieveSchema(testValidator, schema, rootSchema, {
1412+
foo: { isString: false },
1413+
bar: { isString: false },
1414+
}),
1415+
).toEqual({
1416+
...schema,
1417+
properties: {
1418+
foo: {
1419+
type: 'object',
1420+
properties: { isString: { type: 'boolean' }, value: { type: 'number' } },
1421+
},
1422+
bar: {
1423+
[ADDITIONAL_PROPERTY_FLAG]: true,
1424+
type: 'object',
1425+
properties: { isString: { type: 'boolean' }, value: { type: 'number' } },
1426+
},
1427+
},
1428+
});
1429+
});
13411430
it('merges all subschemas that match the patternProperties regex', () => {
13421431
const schema: RJSFSchema = {
13431432
type: 'object',

0 commit comments

Comments
 (0)