From 687e02e7d84dc06957844160761fda90929470ab Mon Sep 17 00:00:00 2001 From: Matheus Michalski Puel Date: Wed, 10 Jan 2024 14:47:02 -0300 Subject: [PATCH] Schema: fix ParseIssue.actual on transformation (#1892) --- .changeset/blue-starfishes-doubt.md | 5 +++ packages/schema/src/Parser.ts | 4 +-- packages/schema/test/ParseResult.test.ts | 40 ++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 .changeset/blue-starfishes-doubt.md diff --git a/.changeset/blue-starfishes-doubt.md b/.changeset/blue-starfishes-doubt.md new file mode 100644 index 00000000000..0ec976214ac --- /dev/null +++ b/.changeset/blue-starfishes-doubt.md @@ -0,0 +1,5 @@ +--- +"@effect/schema": patch +--- + +Schema: fix ParseIssue.actual on transformation diff --git a/packages/schema/src/Parser.ts b/packages/schema/src/Parser.ts index 78695e32c36..10f794a773f 100644 --- a/packages/schema/src/Parser.ts +++ b/packages/schema/src/Parser.ts @@ -368,11 +368,11 @@ const go = (ast: AST.AST, isDecoding: boolean): Parser => { 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, diff --git a/packages/schema/test/ParseResult.test.ts b/packages/schema/test/ParseResult.test.ts index 9932b3be121..e9eb032a9d0 100644 --- a/packages/schema/test/ParseResult.test.ts +++ b/packages/schema/test/ParseResult.test.ts @@ -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") + }) +})