Skip to content

Commit

Permalink
fix: deal with null objects in errors in Form.filterErrorsBasedOnSche…
Browse files Browse the repository at this point in the history
…ma() (#4310)

* fix: deal with null objects in errors in Form.filterErrorsBasedOnSchema()
Fixes #4306 by using `lodash.isNil()` instead of comparing to `undefined`
- Updated `Form.filterErrorsBasedOnSchema()` to use lodash `isNil()` to check if the key is either null or undefined
- Updated the `CHANGELOG.md` accordingly

* - Made name more correct
  • Loading branch information
heath-freenome authored Sep 20, 2024
1 parent 4cb8342 commit d3af307
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 6 additions & 5 deletions packages/core/src/components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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<T> => {
// Removing undefined, null and empty errors.
const filterNilOrEmptyErrors = (errors: any): ErrorSchema<T> => {
_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
Expand Down

0 comments on commit d3af307

Please sign in to comment.