Skip to content

Commit e73b960

Browse files
Fix 5163: stop padding minItems arrays with null under arrayMinItems.populate = 'never' (#5167)
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 0e31f51 commit e73b960

3 files changed

Lines changed: 29 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ should change the heading of the (upcoming) version to include a major version b
2525
## @rjsf/utils
2626

2727
- Added `getDecimalSeparator()` utility to detect locale-specific decimal separators and updated `asNumber()` to parse locale-specific decimal strings, partially fixing [#5148](https://github.com/rjsf-team/react-jsonschema-form/pull/5148)
28+
- Fixed a regression where `getDefaultFormState()` with `experimental_defaultFormStateBehavior.arrayMinItems.populate = 'never'` padded arrays with `null` entries up to `minItems`, fixing [#5163](https://github.com/rjsf-team/react-jsonschema-form/issues/5163)
29+
- Optional arrays with no data are now omitted (unchanged) while required arrays with no data default to `[]`, letting the validator report the `minItems` violation instead of surfacing invalid `null` items
2830

2931
## @rjsf/validator-ata
3032

@@ -2496,7 +2498,7 @@ Move theme snapshot tests into separate package
24962498
- However, if users of @rjsf/antd want to use v5 styling, they need to wrap your application with the `StyleProvider` from `@ant-design/cssinjs`. They need not have to install this package, its a transitive package coming from antd.
24972499

24982500
```tsx
2499-
import { StyleProvider } from "@ant-design/cssinjs";
2501+
import { StyleProvider } from '@ant-design/cssinjs';
25002502

25012503
const Component = () => {
25022504
return (

packages/utils/src/schema/getDefaultFormState.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -719,11 +719,10 @@ export function getArrayDefaults<T = any, S extends StrictRJSFSchema = RJSFSchem
719719
const defaultsLength = Array.isArray(defaults) ? defaults.length : 0;
720720

721721
if (neverPopulate) {
722-
if (shouldMergeDefaultsIntoFormData) {
723-
if (schema.minItems && schema.minItems > defaultsLength) {
724-
const fillerEntries = Array.from({ length: schema.minItems - defaultsLength }, () => null) as T[];
725-
return (defaults ?? []).concat(fillerEntries);
726-
}
722+
if (shouldMergeDefaultsIntoFormData && !required) {
723+
// Optional arrays with no existing data should be omitted entirely rather than defaulted to `[]`.
724+
// Required arrays still fall through to `defaults ?? emptyDefault` below so that `[]` is surfaced,
725+
// letting the validator report `minItems` violations instead of a missing-required-property error.
727726
return defaults;
728727
}
729728
return defaults ?? emptyDefault;

packages/utils/test/schema/getDefaultFormStateTest.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,9 +2376,7 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
23762376

23772377
expect(
23782378
getDefaultFormState(testValidator, schema, undefined, undefined, undefined, defaultFormStateBehavior),
2379-
).toEqual({
2380-
minItemsArray: [null],
2381-
});
2379+
).toEqual({});
23822380
});
23832381

23842382
test('optional array with arrayMinItems.populate = never preserves short formData as-is', () => {
@@ -2432,6 +2430,27 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
24322430
),
24332431
).toEqual({ minItemsArray: ['foo', 'bar'] });
24342432
});
2433+
2434+
test('required array with arrayMinItems.populate = never does not pad minItems with null (issue #5163)', () => {
2435+
const schema: RJSFSchema = {
2436+
type: 'object',
2437+
properties: {
2438+
tags: {
2439+
type: 'array',
2440+
minItems: 1,
2441+
items: { type: 'string' },
2442+
},
2443+
},
2444+
required: ['tags'],
2445+
};
2446+
const defaultFormStateBehavior: Experimental_DefaultFormStateBehavior = {
2447+
arrayMinItems: { populate: 'never' },
2448+
};
2449+
2450+
expect(
2451+
getDefaultFormState(testValidator, schema, undefined, undefined, undefined, defaultFormStateBehavior),
2452+
).toEqual({ tags: [] });
2453+
});
24352454
});
24362455
});
24372456

0 commit comments

Comments
 (0)