Skip to content

Commit f6b0960

Browse files
authored
Backport: Fix TupleWithRest post-rest index drift validation bug (#6097)
1 parent 88a5260 commit f6b0960

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Fix TupleWithRest post-rest validation to check each tail index sequentially.

packages/effect/src/ParseResult.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,15 +1033,15 @@ const go = (ast: AST.AST, isDecoding: boolean): Parser => {
10331033
// handle post rest elements
10341034
// ---------------------------------------------
10351035
for (let j = 0; j < tail.length; j++) {
1036-
i += j
1037-
if (len < i + 1) {
1036+
const index = i + j
1037+
if (len < index + 1) {
10381038
continue
10391039
} else {
1040-
const te = tail[j](input[i], options)
1040+
const te = tail[j](input[index], options)
10411041
if (isEither(te)) {
10421042
if (Either.isLeft(te)) {
10431043
// the input element is present but is not valid
1044-
const e = new Pointer(i, input, te.left)
1044+
const e = new Pointer(index, input, te.left)
10451045
if (allErrors) {
10461046
es.push([stepKey++, e])
10471047
continue
@@ -1052,7 +1052,6 @@ const go = (ast: AST.AST, isDecoding: boolean): Parser => {
10521052
output.push([stepKey++, te.right])
10531053
} else {
10541054
const nk = stepKey++
1055-
const index = i
10561055
if (!queue) {
10571056
queue = []
10581057
}

packages/effect/test/Schema/Schema/Tuple/Tuple.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,21 @@ describe("Tuple", () => {
323323
└─ is missing`
324324
)
325325
})
326+
327+
it("[String] + [Boolean, String, Number, Number] validates every post-rest index", async () => {
328+
const schema = S.Tuple([S.String], S.Boolean, S.String, S.NumberFromString, S.NumberFromString)
329+
330+
await Util.assertions.decoding.fail(
331+
schema,
332+
["a", true, "b", "1", "x"],
333+
`readonly [string, ...boolean[], string, NumberFromString, NumberFromString]
334+
└─ [4]
335+
└─ NumberFromString
336+
└─ Transformation process failure
337+
└─ Unable to decode "x" into a number`
338+
)
339+
await Util.assertions.decoding.succeed(schema, ["a", true, "b", "1", "2"], ["a", true, "b", 1, 2])
340+
})
326341
})
327342

328343
describe("encoding", () => {

0 commit comments

Comments
 (0)