Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
fix attachPropertySignature bug ref #153 (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored Mar 19, 2023
1 parent 780e634 commit 0b86081
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/lazy-panthers-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/schema": patch
---

fix attachPropertySignature bug ref #153
2 changes: 1 addition & 1 deletion src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ export const attachPropertySignature = <K extends PropertyKey, V extends AST.Lit
<I, A extends object>(schema: Schema<I, A>): Schema<I, Spread<A & { readonly [k in K]: V }>> =>
transform<I, A, any>(
schema,
pipe(schema, extend(struct({ [key]: literal(value) }))),
pipe(to(schema), extend(struct({ [key]: literal(value) }))),
(a) => ({ ...a, [key]: value }),
({ [key]: _key, ...rest }) => rest
)
Expand Down
18 changes: 18 additions & 0 deletions test/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
})
})

0 comments on commit 0b86081

Please sign in to comment.