Skip to content

Commit

Permalink
fix: #321 editor not accepting JSON created in a different JavaScript…
Browse files Browse the repository at this point in the history
… realm like an iframe
  • Loading branch information
josdejong committed Oct 18, 2023
1 parent 76dd837 commit 06fb84c
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/lib/utils/typeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ import type { JSONValue } from 'immutable-json-patch'
* Test whether a value is an Object (and not an Array or Class)
*/
export function isObject(value: unknown): value is Record<string, unknown> {
return (
typeof value === 'object' && value !== null && value.constructor === Object // do not match on classes or Array
)
// note that we check constructor.name, not constructor === Object,
// so we can use objects created in a different JS realm like an iframe.
return typeof value === 'object' && value !== null && value.constructor.name === 'Object'
}

/**
* Test whether a value is an Object or an Array (and not a Class)
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function isObjectOrArray(value: unknown): value is Object | Array<unknown> {
// Note that we check constructor.name, not constructor === Object,
// so we can use objects created in a different JS realm like an iframe.
return (
typeof value === 'object' &&
value !== null &&
(value.constructor === Object || value.constructor === Array)
(value.constructor.name === 'Object' || value.constructor.name === 'Array')
)
}

Expand Down

0 comments on commit 06fb84c

Please sign in to comment.