Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema: change the MessageAnnotation type to be non-parametric; now… #2170

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gentle-hounds-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/schema": minor
---

Change the `MessageAnnotation` type to be non-parametric; now it's simply `(u: unknown) => string` to accommodate custom error messages, which can be triggered by any circumstances
4 changes: 2 additions & 2 deletions packages/schema/src/AST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const TypeAnnotationId = Symbol.for("@effect/schema/annotation/Type")
* @category annotations
* @since 1.0.0
*/
export type MessageAnnotation<A> = (a: A) => string
export type MessageAnnotation = (u: unknown) => string

/**
* @category annotations
Expand Down Expand Up @@ -173,7 +173,7 @@ export const getAnnotation: {
* @category annotations
* @since 1.0.0
*/
export const getMessageAnnotation = getAnnotation<MessageAnnotation<unknown>>(
export const getMessageAnnotation = getAnnotation<MessageAnnotation>(
MessageAnnotationId
)

Expand Down
4 changes: 2 additions & 2 deletions packages/schema/src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ const declarePrimitive = <A>(
* @since 1.0.0
*/
export interface DeclareAnnotations<P extends ReadonlyArray<any>, A> extends DocAnnotations {
readonly message?: AST.MessageAnnotation<A>
readonly message?: AST.MessageAnnotation
readonly typeId?: AST.TypeAnnotation | { id: AST.TypeAnnotation; annotation: unknown }
readonly arbitrary?: (...arbitraries: { readonly [K in keyof P]: Arbitrary<P[K]> }) => Arbitrary<A>
readonly pretty?: (...pretties: { readonly [K in keyof P]: Pretty.Pretty<P[K]> }) => Pretty.Pretty<A>
Expand Down Expand Up @@ -1909,7 +1909,7 @@ export const annotations = (annotations: AST.Annotations) => <A, I, R>(self: Sch
* @category annotations
* @since 1.0.0
*/
export const message = (message: AST.MessageAnnotation<unknown>) => <A, I, R>(self: Schema<A, I, R>): Schema<A, I, R> =>
export const message = (message: AST.MessageAnnotation) => <A, I, R>(self: Schema<A, I, R>): Schema<A, I, R> =>
make(AST.setAnnotation(self.ast, AST.MessageAnnotationId, message))

/**
Expand Down
9 changes: 8 additions & 1 deletion packages/schema/test/Schema/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as Option from "effect/Option"
import { describe, expect, it } from "vitest"

describe("Schema > filter", () => {
it("filter/ annotation options", () => {
it("annotation options", () => {
const schema = S.string.pipe(
S.filter((s): s is string => s.length === 1, {
typeId: Symbol.for("Char"),
Expand Down Expand Up @@ -58,4 +58,11 @@ describe("Schema > filter", () => {
└─ b should be equal to a's value ("a")`
)
})

it("custom message", async () => {
const schema = S.string.pipe(S.filter((s): s is string => s.length === 1, {
message: (u) => `invalid ${u}`
}))
await Util.expectDecodeUnknownFailure(schema, null, `invalid null`)
})
})
Loading