diff --git a/.changeset/lazy-panthers-doubt.md b/.changeset/lazy-panthers-doubt.md new file mode 100644 index 000000000..32ebe6f62 --- /dev/null +++ b/.changeset/lazy-panthers-doubt.md @@ -0,0 +1,5 @@ +--- +"@effect/schema": patch +--- + +fix attachPropertySignature bug ref #153 diff --git a/src/Schema.ts b/src/Schema.ts index a05b391ef..29392089b 100644 --- a/src/Schema.ts +++ b/src/Schema.ts @@ -772,7 +772,7 @@ export const attachPropertySignature = (schema: Schema): Schema> => transform( schema, - pipe(schema, extend(struct({ [key]: literal(value) }))), + pipe(to(schema), extend(struct({ [key]: literal(value) }))), (a) => ({ ...a, [key]: value }), ({ [key]: _key, ...rest }) => rest ) diff --git a/test/Schema.ts b/test/Schema.ts index 86d341029..e98f3d6cd 100644 --- a/test/Schema.ts +++ b/test/Schema.ts @@ -571,4 +571,22 @@ describe.concurrent("Schema", () => { }) ).toEqual({ sideLength: 10 }) }) + + it("attachPropertySignature/ with a transformation", () => { + const From = S.struct({ radius: S.number, _isVisible: S.optional(S.boolean) }) + const To = S.struct({ radius: S.number, _isVisible: S.boolean }) + + const Circle = pipe( + S.transformEither(From, To, S.decodeEither(To), ({ _isVisible, ...rest }) => E.right(rest)), + S.attachPropertySignature("_tag", "Circle") + ) + expect(S.decode(Circle)({ radius: 10, _isVisible: true })).toEqual({ + _tag: "Circle", + _isVisible: true, + radius: 10 + }) + expect(S.encode(Circle)({ _tag: "Circle", radius: 10, _isVisible: true })).toEqual({ + radius: 10 + }) + }) })