From d3b092cc0f709737979a538e247573b75264f2b1 Mon Sep 17 00:00:00 2001 From: BenBeattieHood Date: Wed, 21 Dec 2022 09:45:13 +1100 Subject: [PATCH 1/5] Tests for existing 'isObject' functionality --- packages/utils/test/isObject.test.ts | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 packages/utils/test/isObject.test.ts diff --git a/packages/utils/test/isObject.test.ts b/packages/utils/test/isObject.test.ts new file mode 100644 index 0000000000..c0e6ddf7d3 --- /dev/null +++ b/packages/utils/test/isObject.test.ts @@ -0,0 +1,36 @@ +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 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); + }); + }); +}); \ No newline at end of file From 4bb6a1a32453d0d19d89691ccf145ea39c464c00 Mon Sep 17 00:00:00 2001 From: Ben Beattie-Hood Date: Tue, 13 Dec 2022 09:16:58 +1100 Subject: [PATCH 2/5] 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. --- CHANGELOG.md | 1 + packages/utils/src/isObject.ts | 3 +++ packages/utils/test/isObject.test.ts | 4 ++++ 3 files changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4812d51571..6c9a4fdc2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ should change the heading of the (upcoming) version to include a major version b ## @rjsf/core - Added `ref` definition to `ThemeProps` fixing [#2135](https://github.com/rjsf-team/react-jsonschema-form/issues/2135) - Updated the `onChange` handler in `Form` to use the new `preventDuplicates` mode of `mergeObjects()` when merging `extraErrors` when live validation is off, fixing [#3169](https://github.com/rjsf-team/react-jsonschema-form/issues/3169) +- Fix isObject to correctly identify 'Date' as not mergable with Object default value. ## @rjsf/material-ui - Fix RangeWidget missing htmlFor and schema.title [#3281](https://github.com/rjsf-team/react-jsonschema-form/pull/3281) 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 index c0e6ddf7d3..68870ae59b 100644 --- a/packages/utils/test/isObject.test.ts +++ b/packages/utils/test/isObject.test.ts @@ -25,6 +25,10 @@ describe('isObject()', () => { 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); }); From 730a4024ffc264404522bae7eb101bafa75b27a7 Mon Sep 17 00:00:00 2001 From: BenBeattieHood Date: Wed, 21 Dec 2022 11:11:56 +1100 Subject: [PATCH 3/5] Corrected changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c9a4fdc2f..3c2159767e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,7 +34,6 @@ should change the heading of the (upcoming) version to include a major version b ## @rjsf/core - Added `ref` definition to `ThemeProps` fixing [#2135](https://github.com/rjsf-team/react-jsonschema-form/issues/2135) - Updated the `onChange` handler in `Form` to use the new `preventDuplicates` mode of `mergeObjects()` when merging `extraErrors` when live validation is off, fixing [#3169](https://github.com/rjsf-team/react-jsonschema-form/issues/3169) -- Fix isObject to correctly identify 'Date' as not mergable with Object default value. ## @rjsf/material-ui - Fix RangeWidget missing htmlFor and schema.title [#3281](https://github.com/rjsf-team/react-jsonschema-form/pull/3281) @@ -46,6 +45,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 mergable with Object default value. ## 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) From 428903a0401cf476a07977566eb4eddb7dd86ca0 Mon Sep 17 00:00:00 2001 From: Heath C <51679588+heath-freenome@users.noreply.github.com> Date: Tue, 20 Dec 2022 16:43:51 -0800 Subject: [PATCH 4/5] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c2159767e..32eb92a41a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,7 +45,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 mergable with Object default value. +- 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) From dcac12f54a7a7143ec725ebc860caee936dd52b5 Mon Sep 17 00:00:00 2001 From: BenBeattieHood Date: Wed, 21 Dec 2022 15:08:27 +1100 Subject: [PATCH 5/5] Lint formatting tests --- packages/utils/test/isObject.test.ts | 41 +++++++++++----------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/packages/utils/test/isObject.test.ts b/packages/utils/test/isObject.test.ts index 68870ae59b..c200e83d04 100644 --- a/packages/utils/test/isObject.test.ts +++ b/packages/utils/test/isObject.test.ts @@ -1,40 +1,31 @@ -import { isObject } from '../src'; +import { isObject } from "../src"; -const NON_OBJECTS = [ - 'string', - 10, - NaN, - true, - null, - undefined, -]; +const NON_OBJECTS = ["string", 10, NaN, true, null, undefined]; -const OBJECTS = [ - { plain: 'object' }, - new Object(), - new Map(), -]; +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); - }); +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', () => { + 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', () => { + 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 false when an array is provided", () => { + expect(isObject(["foo"])).toBe(false); }); - it('returns true when an object is provided', () => { + it("returns true when an object is provided", () => { OBJECTS.forEach((object: any) => { expect(isObject(object)).toBe(true); }); }); -}); \ No newline at end of file +});