diff --git a/CHANGELOG.md b/CHANGELOG.md index cfba570c5a..920ccfdeb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ should change the heading of the (upcoming) version to include a major version b ## @rjsf/core - Updated `SchemaField` to pass `required` flag to `_AnyOfField`/`_OneOfField` +- Updated `Form` to deal with null objects in `filterErrorsBasedOnSchema()`, fixing [#4306](https://github.com/rjsf-team/react-jsonschema-form/issues/4306) ## Dev / docs / playground diff --git a/packages/core/src/components/Form.tsx b/packages/core/src/components/Form.tsx index aa8bc60ab5..7df7891f71 100644 --- a/packages/core/src/components/Form.tsx +++ b/packages/core/src/components/Form.tsx @@ -37,6 +37,7 @@ import { import _forEach from 'lodash/forEach'; import _get from 'lodash/get'; import _isEmpty from 'lodash/isEmpty'; +import _isNil from 'lodash/isNil'; import _pick from 'lodash/pick'; import _toPath from 'lodash/toPath'; @@ -603,18 +604,18 @@ export default class Form< if (resolvedSchema?.type !== 'object' && resolvedSchema?.type !== 'array') { filteredErrors.__errors = schemaErrors.__errors; } - // Removing undefined and empty errors. - const filterUndefinedErrors = (errors: any): ErrorSchema => { + // Removing undefined, null and empty errors. + const filterNilOrEmptyErrors = (errors: any): ErrorSchema => { _forEach(errors, (errorAtKey, errorKey: keyof typeof errors) => { - if (errorAtKey === undefined) { + if (_isNil(errorAtKey)) { delete errors[errorKey]; } else if (typeof errorAtKey === 'object' && !Array.isArray(errorAtKey.__errors)) { - filterUndefinedErrors(errorAtKey); + filterNilOrEmptyErrors(errorAtKey); } }); return errors; }; - return filterUndefinedErrors(filteredErrors); + return filterNilOrEmptyErrors(filteredErrors); } /** Function to handle changes made to a field in the `Form`. This handler receives an entirely new copy of the