Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}
heath-freenome marked this conversation as resolved.
Show resolved Hide resolved
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);
});
});
});