Skip to content

Commit

Permalink
isObject should return false for Date as well as File, null, and Array (
Browse files Browse the repository at this point in the history
rjsf-team#3292)

* Tests for existing 'isObject' functionality

* isObject should return false for Date as well as File, null, and Array

Without this, assigning dates to the form's value instead merges them with a default value for objects of `{}` (in mergeDefaultsWithFormData), which stops them being dates.

* Corrected changelog entry

* Update CHANGELOG.md

* Lint formatting tests

Co-authored-by: Heath C <51679588+heath-freenome@users.noreply.github.com>
  • Loading branch information
2 people authored and shijistar committed Jun 8, 2023
1 parent f95d7ae commit 7a2f5dc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -46,6 +46,7 @@ should change the heading of the (upcoming) version to include a major version b
- Updated `computedDefaults` (used by `getDefaultFormState`) to skip saving the computed default if it's an empty object unless `includeUndefinedValues` is truthy, fixing [#2150](https://github.com/rjsf-team/react-jsonschema-form/issues/2150) and [#2708](https://github.com/rjsf-team/react-jsonschema-form/issues/2708)
- Expanded the `getDefaultFormState` util's `includeUndefinedValues` prop to accept a boolean or `"excludeObjectChildren"` if it does not want to include undefined values in nested objects
- Updated `mergeObjects` to add new `preventDuplicates` mode when concatenating arrays so that only unique values from the source object array are copied to the destination object array
- Fix `isObject` to correctly identify 'Date' as not an object, similar to 'File', thus preventing them from being merged with Object default values.

## Dev / docs / playground
- Removed extraneous leading space on the examples in the validation documentation, fixing [#3282](https://github.com/rjsf-team/react-jsonschema-form/issues/3282)
Expand Down
3 changes: 3 additions & 0 deletions packages/utils/src/isObject.ts
Expand Up @@ -8,5 +8,8 @@ export default function isObject(thing: any) {
if (typeof File !== "undefined" && thing instanceof File) {
return false;
}
if (typeof Date !== "undefined" && thing instanceof Date) {
return false;
}
return typeof thing === "object" && thing !== null && !Array.isArray(thing);
}
31 changes: 31 additions & 0 deletions packages/utils/test/isObject.test.ts
@@ -0,0 +1,31 @@
import { isObject } from "../src";

const NON_OBJECTS = ["string", 10, NaN, true, null, undefined];

const OBJECTS = [{ plain: "object" }, new Object(), new Map()];

describe("isObject()", () => {
it("returns false when a non-object is provided", () => {
NON_OBJECTS.forEach(
(nonObject: string | number | boolean | null | undefined) => {
expect(isObject(nonObject)).toBe(false);
}
);
});
it("returns false when a File is provided", () => {
const file = new File(["test"], "test.txt");
expect(isObject(file)).toBe(false);
});
it("returns false when a Date is provided", () => {
const date = new Date();
expect(isObject(date)).toBe(false);
});
it("returns false when an array is provided", () => {
expect(isObject(["foo"])).toBe(false);
});
it("returns true when an object is provided", () => {
OBJECTS.forEach((object: any) => {
expect(isObject(object)).toBe(true);
});
});
});

0 comments on commit 7a2f5dc

Please sign in to comment.