Skip to content

Commit ef743e0

Browse files
test(contracts): pin that sparse arrays count as holes
Effect's array parser already turns a sparse array's missing index into `undefined`, so `Schema.is` and `make` rejected `new Array(1)` before this. The hole check now walks every index itself instead of relying on that, and a test pins sparse arrays alongside explicit `undefined`. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 58a4224 commit ef743e0

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

‎packages/contracts/src/baseSchemas.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ describe("ForwardCompatibleArray", () => {
8181

8282
it("does not accept holes as a decoded value", () => {
8383
expect(Schema.is(Named)([undefined])).toBe(false);
84+
// A sparse array's missing index is a hole too.
85+
const sparse: Array<{ name: string }> = [{ name: "a" }];
86+
sparse.length = 2;
87+
expect(Schema.is(Named)(sparse)).toBe(false);
88+
expect(() => Named.make(sparse)).toThrow();
8489
expect(Schema.is(Named)([{ name: "a" }])).toBe(true);
8590
});
8691
});

‎packages/contracts/src/baseSchemas.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,13 @@ export const ForwardCompatibleArray = <Element extends Schema.Top>(element: Elem
145145
// `Schema.is` and `make` still reject an array that does. Aborts, so a
146146
// later check on the array never sees a hole.
147147
Schema.makeFilter(
148-
(values) => values.every((value) => value !== undefined),
148+
(values) => {
149+
// Every index, not `every`, which skips the holes of a sparse array.
150+
for (let index = 0; index < values.length; index++) {
151+
if (values[index] === undefined) return false;
152+
}
153+
return true;
154+
},
149155
{ expected: "an array without holes" },
150156
true,
151157
),

0 commit comments

Comments
 (0)