Skip to content

Commit

Permalink
Schema: fix ParseIssue.actual on transformation (#1892)
Browse files Browse the repository at this point in the history
  • Loading branch information
matheuspuel authored Jan 10, 2024
1 parent 2198fa4 commit 687e02e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-starfishes-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/schema": patch
---

Schema: fix ParseIssue.actual on transformation
4 changes: 2 additions & 2 deletions packages/schema/src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,11 @@ const go = (ast: AST.AST, isDecoding: boolean): Parser<any, any> => {
ParseResult.flatMap(
ParseResult.mapLeft(
transform(a, options ?? defaultParseOption, ast),
(e) => ParseResult.transform(ast, a, "Transformation", e.error)
(e) => ParseResult.transform(ast, i1, "Transformation", e.error)
),
(i2) =>
ParseResult.mapLeft(to(i2, options), (e) =>
ParseResult.transform(ast, i2, isDecoding ? "To" : "From", e))
ParseResult.transform(ast, i1, isDecoding ? "To" : "From", e))
)
),
i1,
Expand Down
40 changes: 40 additions & 0 deletions packages/schema/test/ParseResult.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,43 @@ describe("ParseResult", () => {
).toStrictEqual(Exit.succeed(2))
})
})

describe("ParseIssue.actual", () => {
it("transform decode", () => {
const result = S.decodeEither(S.transformOrFail(
S.NumberFromString,
S.boolean,
(_) => ParseResult.fail(ParseResult.forbidden(_)),
(_) => ParseResult.fail(ParseResult.forbidden(_))
))("1")
if (Either.isRight(result)) throw new Error("Expected failure")
expect(result.left.error.actual).toEqual("1")
expect((result.left.error as ParseResult.Transform).error.actual).toEqual(1)
})

it("transform encode", () => {
const result = S.encodeEither(S.transformOrFail(
S.boolean,
S.NumberFromString,
(_) => ParseResult.fail(ParseResult.forbidden(_)),
(_) => ParseResult.fail(ParseResult.forbidden(_))
))(1)
if (Either.isRight(result)) throw new Error("Expected failure")
expect(result.left.error.actual).toEqual(1)
expect((result.left.error as ParseResult.Transform).error.actual).toEqual("1")
})

it("compose decode", () => {
const result = S.decodeEither(S.compose(S.NumberFromString, S.negative()(S.number)))("1")
if (Either.isRight(result)) throw new Error("Expected failure")
expect(result.left.error.actual).toEqual("1")
expect((result.left.error as ParseResult.Transform).error.actual).toEqual(1)
})

it("compose encode", () => {
const result = S.encodeEither(S.compose(S.length(5)(S.string), S.NumberFromString))(1)
if (Either.isRight(result)) throw new Error("Expected failure")
expect(result.left.error.actual).toEqual(1)
expect((result.left.error as ParseResult.Transform).error.actual).toEqual("1")
})
})

0 comments on commit 687e02e

Please sign in to comment.