Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions ark/schema/roots/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
domainOf,
getBuiltinNameOfConstructor,
hasKey,
isArray,
objectKindDescriptions,
objectKindOrDomainOf,
throwParseError,
Expand Down Expand Up @@ -144,12 +145,20 @@ export class ProtoNode extends InternalBasis<Proto.Declaration> {
private readonly requiresInvalidDateCheck =
this.proto === Date && !this.dateAllowsInvalid

// Array.isArray is realm-safe; `instanceof Array` rejects arrays from other
// windows/vm contexts (https://github.com/arktypeio/arktype/issues/1597).
private readonly isArrayProto = this.proto === Array

traverseAllows: TraverseAllows =
this.requiresInvalidDateCheck ?
data => data instanceof Date && data.toString() !== "Invalid Date"
: data => data instanceof this.proto
: this.isArrayProto ? data => isArray(data)
: data => data instanceof this.proto

compiledCondition = `data instanceof ${this.serializedConstructor}${this.requiresInvalidDateCheck ? ` && data.toString() !== "Invalid Date"` : ""}`
compiledCondition =
this.isArrayProto ? `Array.isArray(data)` : (
`data instanceof ${this.serializedConstructor}${this.requiresInvalidDateCheck ? ` && data.toString() !== "Invalid Date"` : ""}`
)
compiledNegation = `!(${this.compiledCondition})`

protected innerToJsonSchema(ctx: ToJsonSchema.Context): JsonSchema {
Expand Down
16 changes: 16 additions & 0 deletions ark/type/__tests__/arrays/array.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { attest, contextualize } from "@ark/attest"
import { scope, type } from "arktype"
import { createContext, runInContext } from "node:vm"
import { incompleteArrayTokenMessage } from "arktype/internal/parser/shift/operator/operator.ts"
import {
multipleVariadicMesage,
Expand Down Expand Up @@ -281,4 +282,19 @@ value at [1] must be a number (was boolean)`)
.snap(`value at [0] must be a string (was a number)
value at [1] must be a string (was a number)`)
})

it("accepts arrays from another realm", () => {
const foreign = runInContext("[1, 2, 3]", createContext())
// Same value identity check would fail: foreign instanceof Array === false
attest(Array.isArray(foreign)).equals(true)
attest(foreign instanceof Array).equals(false)

const T = type("number[]")
attest(T.allows(foreign)).equals(true)
// Don't use .equals on the array itself — foreign-realm arrays have a
// different constructor, which attest treats as unequal.
const result = T(foreign)
attest(result === foreign).equals(true)
attest([...(result as number[])]).equals([1, 2, 3])
})
})
4 changes: 4 additions & 0 deletions ark/util/objectKinds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ type instantiableObjectKind<data extends object> = {
export const objectKindOf = <data extends object>(
data: data
): objectKindOf<data> | undefined => {
// Realm-safe: cross-window/vm arrays fail `instanceof Array` but pass
// Array.isArray (https://github.com/arktypeio/arktype/issues/1597).
if (Array.isArray(data)) return "Array" as never

let prototype: Partial<Object> | null = Object.getPrototypeOf(data)
while (
prototype?.constructor &&
Expand Down
Loading