From 24086163b60b09cc6d0885bd565ef080dcbe866b Mon Sep 17 00:00:00 2001 From: Giulio Canti Date: Fri, 13 Dec 2024 14:15:39 +0100 Subject: [PATCH] =?UTF-8?q?Arbitrary:=20fix=20bug=20related=20to=20refinem?= =?UTF-8?q?ents=20of=20declarations=20raising=20a=20w=E2=80=A6=20(#4137)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/little-baboons-count.md | 5 +++++ packages/effect/src/Arbitrary.ts | 5 ++++- packages/effect/src/Schema.ts | 4 ++-- packages/effect/test/Schema/Arbitrary/Arbitrary.test.ts | 7 +++++++ 4 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 .changeset/little-baboons-count.md diff --git a/.changeset/little-baboons-count.md b/.changeset/little-baboons-count.md new file mode 100644 index 0000000000..2bb7e897f4 --- /dev/null +++ b/.changeset/little-baboons-count.md @@ -0,0 +1,5 @@ +--- +"effect": patch +--- + +Arbitrary: fix bug where refinements in declarations raised an incorrect missing annotation error, closes #4136 diff --git a/packages/effect/src/Arbitrary.ts b/packages/effect/src/Arbitrary.ts index 69d7730c63..494c356730 100644 --- a/packages/effect/src/Arbitrary.ts +++ b/packages/effect/src/Arbitrary.ts @@ -268,6 +268,9 @@ const go = ( return hook.value(ctx) } } + if (AST.isDeclaration(ast)) { + throw new Error(errors_.getArbitraryMissingAnnotationErrorMessage(path, ast)) + } const op = toOp(ast, ctx, path) switch (op._tag) { case "Succeed": @@ -289,7 +292,7 @@ export const toOp = ( ): Op => { switch (ast._tag) { case "Declaration": - throw new Error(errors_.getArbitraryMissingAnnotationErrorMessage(path, ast)) + return new Succeed(go(ast, ctx, path)) case "Literal": return new Succeed((fc) => fc.constant(ast.literal)) case "UniqueSymbol": diff --git a/packages/effect/src/Schema.ts b/packages/effect/src/Schema.ts index 82f6d5b5d9..d75e95898c 100644 --- a/packages/effect/src/Schema.ts +++ b/packages/effect/src/Schema.ts @@ -6233,8 +6233,8 @@ export class DateFromSelf extends declare( { identifier: "DateFromSelf", description: "a potentially invalid Date instance", - pretty: (): pretty_.Pretty => (date) => `new Date(${JSON.stringify(date)})`, - arbitrary: (): LazyArbitrary => (fc) => fc.date({ noInvalidDate: false }), + pretty: () => (date) => `new Date(${JSON.stringify(date)})`, + arbitrary: () => (fc) => fc.date({ noInvalidDate: false }), equivalence: () => Equivalence.Date } ) {} diff --git a/packages/effect/test/Schema/Arbitrary/Arbitrary.test.ts b/packages/effect/test/Schema/Arbitrary/Arbitrary.test.ts index 2520ee72fd..f467fcded5 100644 --- a/packages/effect/test/Schema/Arbitrary/Arbitrary.test.ts +++ b/packages/effect/test/Schema/Arbitrary/Arbitrary.test.ts @@ -521,6 +521,13 @@ details: Generating an Arbitrary for this schema requires at least one enum`) }) describe("Refinement", () => { + describe("declaration filters", () => { + it("ValidDateFromSelf", () => { + const schema = S.ValidDateFromSelf + expectValidArbitrary(schema) + }) + }) + describe("array filters", () => { it("minItems", () => { const schema = S.Array(S.String).pipe(S.minItems(2))