From aa58b0095e7761ad314f6dd7a4c564187ded37f9 Mon Sep 17 00:00:00 2001 From: Ben Beattie-Hood Date: Thu, 22 Dec 2022 04:32:23 +1100 Subject: [PATCH] isObject should return false for Date as well as File, null, and Array (#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> --- CHANGELOG.md | 1 + packages/utils/src/isObject.ts | 3 +++ packages/utils/test/isObject.test.ts | 31 ++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 packages/utils/test/isObject.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index ec94d78d31..d25d85ee16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/packages/utils/src/isObject.ts b/packages/utils/src/isObject.ts index 8c447ffe1a..713a659b84 100644 --- a/packages/utils/src/isObject.ts +++ b/packages/utils/src/isObject.ts @@ -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); } diff --git a/packages/utils/test/isObject.test.ts b/packages/utils/test/isObject.test.ts new file mode 100644 index 0000000000..c200e83d04 --- /dev/null +++ b/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); + }); + }); +});