Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ should change the heading of the (upcoming) version to include a major version b

-->

# 6.0.0-beta.8

## @rjsf/util

- Fixed form data propagation with `patternProperties` [#4617](https://github.com/rjsf-team/react-jsonschema-form/pull/4617)

# 6.0.0-beta.7

## @rjsf/core
Expand Down
7 changes: 4 additions & 3 deletions packages/utils/src/findSchemaDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { GenericObjectType, RJSFSchema, StrictRJSFSchema } from './types';
import isObject from 'lodash/isObject';
import isEmpty from 'lodash/isEmpty';
import UriResolver from 'fast-uri';
import get from 'lodash/get';

/** Looks for the `$id` pointed by `ref` in the schema definitions embedded in
* a JSON Schema bundle
Expand Down Expand Up @@ -51,15 +52,15 @@ export function splitKeyElementFromObject(key: string, object: GenericObjectType
* @param $ref - The ref string for which the schema definition is desired
* @param [rootSchema={}] - The root schema in which to search for the definition
* @param recurseList - List of $refs already resolved to prevent recursion
* @param baseURI - The base URI to be used for resolving relative references
* @param [baseURI=rootSchema['$id']] - The base URI to be used for resolving relative references
* @returns - The sub-schema within the `rootSchema` which matches the `$ref` if it exists
* @throws - Error indicating that no schema for that reference could be resolved
*/
export function findSchemaDefinitionRecursive<S extends StrictRJSFSchema = RJSFSchema>(
$ref?: string,
rootSchema: S = {} as S,
recurseList: string[] = [],
baseURI: string | undefined = ID_KEY in rootSchema ? rootSchema[ID_KEY] : undefined,
baseURI: string | undefined = get(rootSchema, [ID_KEY]),
): S {
const ref = $ref || '';
let current: S | undefined = undefined;
Expand Down Expand Up @@ -123,7 +124,7 @@ export function findSchemaDefinitionRecursive<S extends StrictRJSFSchema = RJSFS
export default function findSchemaDefinition<S extends StrictRJSFSchema = RJSFSchema>(
$ref?: string,
rootSchema: S = {} as S,
baseURI: string | undefined = ID_KEY in rootSchema ? rootSchema[ID_KEY] : undefined,
baseURI: string | undefined = get(rootSchema, [ID_KEY]),
): S {
const recurseList: string[] = [];
return findSchemaDefinitionRecursive($ref, rootSchema, recurseList, baseURI);
Expand Down
6 changes: 3 additions & 3 deletions packages/utils/src/schema/retrieveSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export function resolveReference<T = any, S extends StrictRJSFSchema = RJSFSchem
* @param schema - The schema for which resolving all references is desired
* @param rootSchema - The root schema that will be forwarded to all the APIs
* @param recurseList - List of $refs already resolved to prevent recursion
* @param baseURI - The base URI to be used for resolving relative references
* @param [baseURI] - The base URI to be used for resolving relative references
* @returns - given schema will all references resolved or the original schema if no internal `$refs` were resolved
*/
export function resolveAllReferences<S extends StrictRJSFSchema = RJSFSchema>(
Expand Down Expand Up @@ -432,7 +432,7 @@ export function stubExistingAdditionalProperties<
validator,
{ allOf: Object.values(matchingProperties) } as S,
rootSchema,
formData as T,
get(formData, [key]) as T,
experimental_customMergeAllOf,
);
set(schema.properties, [key, ADDITIONAL_PROPERTY_FLAG], true);
Expand Down Expand Up @@ -578,7 +578,7 @@ export function retrieveSchemaInternal<
validator,
{ allOf: [schema.properties[key], ...Object.values(matchingProperties)] } as S,
rootSchema,
rawFormData as T,
get(rawFormData, [key]) as T,
experimental_customMergeAllOf,
);
}
Expand Down
89 changes: 89 additions & 0 deletions packages/utils/test/schema/retrieveSchemaTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,95 @@ export default function retrieveSchemaTest(testValidator: TestValidatorType) {
});
});
describe('withPatternProperties()', () => {
it('validates schemas with conditions inside patternProperties', () => {
const schema: RJSFSchema = {
type: 'object',
properties: {
foo: {
type: 'object',
},
},
patternProperties: {
'^[a-z]+$': {
type: 'object',
properties: {
isString: { type: 'boolean' },
},
allOf: [
{
if: {
properties: { isString: { const: true } },
},
then: {
properties: {
value: {
type: 'string',
},
},
},
},
{
if: {
properties: { isString: { const: false } },
},
then: {
properties: {
value: {
type: 'number',
},
},
},
},
],
},
},
};
const rootSchema: RJSFSchema = { definitions: {} };
testValidator.setReturnValues({
isValid: [true, false, true, false],
});
expect(
retrieveSchema(testValidator, schema, rootSchema, {
foo: { isString: true },
bar: { isString: true },
}),
).toEqual({
...schema,
properties: {
foo: {
type: 'object',
properties: { isString: { type: 'boolean' }, value: { type: 'string' } },
},
bar: {
[ADDITIONAL_PROPERTY_FLAG]: true,
type: 'object',
properties: { isString: { type: 'boolean' }, value: { type: 'string' } },
},
},
});
testValidator.setReturnValues({
isValid: [false, true, false, true],
});
expect(
retrieveSchema(testValidator, schema, rootSchema, {
foo: { isString: false },
bar: { isString: false },
}),
).toEqual({
...schema,
properties: {
foo: {
type: 'object',
properties: { isString: { type: 'boolean' }, value: { type: 'number' } },
},
bar: {
[ADDITIONAL_PROPERTY_FLAG]: true,
type: 'object',
properties: { isString: { type: 'boolean' }, value: { type: 'number' } },
},
},
});
});
it('merges all subschemas that match the patternProperties regex', () => {
const schema: RJSFSchema = {
type: 'object',
Expand Down