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

Commit

Permalink
Schema: add annotations argument to attachPropertySignature (#679)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored Dec 24, 2023
1 parent 6b9585b commit 0f8a8f1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/thin-ways-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/schema": patch
---

Schema: add annotations argument to `attachPropertySignature`
6 changes: 4 additions & 2 deletions docs/modules/Schema.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -1855,12 +1855,14 @@ but rather maps to another schema, for example when you want to add a discrimina
export declare const attachPropertySignature: {
<K extends PropertyKey, V extends symbol | AST.LiteralValue>(
key: K,
value: V
value: V,
options?: DocAnnotations
): <I, A extends object>(schema: Schema<I, A>) => Schema<I, Simplify<A & { readonly [k in K]: V }>>
<I, A, K extends PropertyKey, V extends symbol | AST.LiteralValue>(
schema: Schema<I, A>,
key: K,
value: V
value: V,
options?: DocAnnotations
): Schema<I, Simplify<A & { readonly [k in K]: V }>>
}
```
Expand Down
54 changes: 31 additions & 23 deletions src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1481,39 +1481,47 @@ export const transformLiterals = <
export const attachPropertySignature: {
<K extends PropertyKey, V extends AST.LiteralValue | symbol>(
key: K,
value: V
value: V,
options?: DocAnnotations
): <I, A extends object>(
schema: Schema<I, A>
) => Schema<I, Simplify<A & { readonly [k in K]: V }>>
<I, A, K extends PropertyKey, V extends AST.LiteralValue | symbol>(
schema: Schema<I, A>,
key: K,
value: V
value: V,
options?: DocAnnotations
): Schema<I, Simplify<A & { readonly [k in K]: V }>>
} = dual(3, <I, A, K extends PropertyKey, V extends AST.LiteralValue | symbol>(
schema: Schema<I, A>,
key: K,
value: V
): Schema<I, Simplify<A & { readonly [k in K]: V }>> =>
make(AST.createTransform(
schema.ast,
extend(
} = dual(
(args) => isSchema(args[0]),
<I, A, K extends PropertyKey, V extends AST.LiteralValue | symbol>(
schema: Schema<I, A>,
key: K,
value: V,
options?: DocAnnotations
): Schema<I, Simplify<A & { readonly [k in K]: V }>> => {
const attached = extend(
to(schema),
struct({ [key]: Predicate.isSymbol(value) ? uniqueSymbol(value) : literal(value) })
).ast,
AST.createTypeLiteralTransformation(
[
AST.createPropertySignatureTransform(
key,
key,
AST.createFinalPropertySignatureTransformation(
() => Option.some(value),
() => Option.none()
).ast
return make(AST.createTransform(
schema.ast,
options ? AST.mergeAnnotations(attached, toAnnotations(options)) : attached,
AST.createTypeLiteralTransformation(
[
AST.createPropertySignatureTransform(
key,
key,
AST.createFinalPropertySignatureTransformation(
() => Option.some(value),
() => Option.none()
)
)
)
]
)
)))
]
)
))
}
)

const toAnnotations = <A>(
options?: FilterAnnotations<A>
Expand Down
18 changes: 18 additions & 0 deletions test/Schema/attachPropertySignature.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,22 @@ describe("Schema/attachPropertySignature", () => {
radius: 10
})
})

it("annotations", async () => {
const schema1 = S.struct({
a: S.string
}).pipe(
S.attachPropertySignature("_tag", "a", { identifier: "MyIdentifier" })
)
await Util.expectEncodeFailure(schema1, null as any, "Expected MyIdentifier, actual null")
const schema2 = S.attachPropertySignature(
S.struct({
a: S.string
}),
"_tag",
"a",
{ identifier: "MyIdentifier" }
)
await Util.expectEncodeFailure(schema2, null as any, "Expected MyIdentifier, actual null")
})
})

0 comments on commit 0f8a8f1

Please sign in to comment.