Skip to content

Commit bb64e56

Browse files
authored
Bug: Schema array with nested dependent fixed-length default value issue. (#4860)
* Fixed issue with schema array with nested dependent fixed-length. * fixed failing tests in validator-ajv8
1 parent 17cbe12 commit bb64e56

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

CHANGELOG.md

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

19+
# 6.1.3
20+
21+
## @rjsf/utils
22+
23+
- Fixed issue with schema array with nested dependent fixed-length, fixing [#3754](https://github.com/rjsf-team/react-jsonschema-form/issues/3754)
24+
25+
1926
# 6.1.2
2027

2128
## @rjsf/antd

packages/utils/src/schema/getDefaultFormState.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,12 +614,14 @@ export function getArrayDefaults<T = any, S extends StrictRJSFSchema = RJSFSchem
614614
if (Array.isArray(defaults)) {
615615
defaults = defaults.map((item, idx) => {
616616
const schemaItem: S = getInnerSchemaForArrayItem<S>(schema, AdditionalItemsHandling.Fallback, idx);
617+
const itemFormData = Array.isArray(rawFormData) ? rawFormData[idx] : undefined;
617618
return computeDefaults<T, S, F>(validator, schemaItem, {
618619
rootSchema,
619620
_recurseList,
620621
experimental_defaultFormStateBehavior,
621622
experimental_customMergeAllOf,
622623
parentDefaults: item,
624+
rawFormData: itemFormData,
623625
required,
624626
shouldMergeDefaultsIntoFormData,
625627
initialDefaultsGenerated,

packages/utils/test/schema/getDefaultFormStateTest.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,6 +1870,107 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
18701870
});
18711871
});
18721872

1873+
describe('array with nested dependent fixed-length array schema', () => {
1874+
const schema: RJSFSchema = {
1875+
items: [
1876+
{
1877+
dependencies: {
1878+
checkbox: {
1879+
if: {
1880+
properties: {
1881+
checkbox: {
1882+
const: true,
1883+
},
1884+
},
1885+
},
1886+
then: {
1887+
properties: {
1888+
inner_array: {
1889+
items: [
1890+
{
1891+
title: 'Fixed Item',
1892+
type: 'string',
1893+
},
1894+
],
1895+
title: 'Inner Fixed-Length Array',
1896+
type: 'array',
1897+
},
1898+
},
1899+
},
1900+
},
1901+
},
1902+
properties: {
1903+
checkbox: {
1904+
title: 'Dependency Checkbox',
1905+
type: 'boolean',
1906+
},
1907+
},
1908+
title: 'Outer Item',
1909+
type: 'object',
1910+
},
1911+
],
1912+
title: 'Outer Fixed Length Array',
1913+
type: 'array',
1914+
};
1915+
const formData = [{ checkbox: true }];
1916+
const includeUndefinedValues = 'excludeObjectChildren';
1917+
const expected = [
1918+
{
1919+
checkbox: true,
1920+
inner_array: [undefined],
1921+
},
1922+
];
1923+
1924+
test('getDefaultFormState', () => {
1925+
expect(getDefaultFormState(testValidator, schema, formData, undefined, includeUndefinedValues)).toEqual(
1926+
expected,
1927+
);
1928+
});
1929+
1930+
test('computeDefaults', () => {
1931+
expect(
1932+
computeDefaults(testValidator, schema, {
1933+
rawFormData: formData,
1934+
includeUndefinedValues,
1935+
shouldMergeDefaultsIntoFormData: true,
1936+
}),
1937+
).toEqual(expected);
1938+
});
1939+
1940+
test('getDefaultBasedOnSchemaType', () => {
1941+
expect(
1942+
getDefaultBasedOnSchemaType(
1943+
testValidator,
1944+
schema,
1945+
{
1946+
includeUndefinedValues,
1947+
},
1948+
[
1949+
{
1950+
checkbox: true,
1951+
},
1952+
],
1953+
),
1954+
).toEqual(expected);
1955+
});
1956+
1957+
test('getArrayDefaults', () => {
1958+
expect(
1959+
getArrayDefaults(
1960+
testValidator,
1961+
schema,
1962+
{
1963+
includeUndefinedValues,
1964+
},
1965+
[
1966+
{
1967+
checkbox: true,
1968+
},
1969+
],
1970+
),
1971+
).toEqual(expected);
1972+
});
1973+
});
18731974
describe('an invalid array schema', () => {
18741975
const schema: RJSFSchema = {
18751976
type: 'array',

0 commit comments

Comments
 (0)