From f409136f46232c3dec3148ad21cf6161760f55dd Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Tue, 10 Dec 2024 15:53:00 +0000 Subject: [PATCH 1/3] Support template literals in Schema.Config --- .changeset/good-pigs-roll.md | 5 +++++ packages/effect/dtslint/Schema.ts | 16 ++++++++++++++++ packages/effect/src/Schema.ts | 4 ++-- .../test/Schema/Schema/Config/Config.test.ts | 13 +++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 .changeset/good-pigs-roll.md diff --git a/.changeset/good-pigs-roll.md b/.changeset/good-pigs-roll.md new file mode 100644 index 00000000000..332d6e23628 --- /dev/null +++ b/.changeset/good-pigs-roll.md @@ -0,0 +1,5 @@ +--- +"effect": minor +--- + +Support template literals in Schema.Config diff --git a/packages/effect/dtslint/Schema.ts b/packages/effect/dtslint/Schema.ts index 70cbafe9d62..c2478e7f43e 100644 --- a/packages/effect/dtslint/Schema.ts +++ b/packages/effect/dtslint/Schema.ts @@ -2895,3 +2895,19 @@ S.NullOr(S.Never) // $ExpectType NullishOr S.NullishOr(S.Never) + +// --------------------------------------------- +// Config +// --------------------------------------------- + +// $ExpectType Config +S.Config("A", S.String) + +// $ExpectType Config +S.Config("A", S.BooleanFromString) + +// $ExpectType Config<`a${string}`> +S.Config("A", S.TemplateLiteral(S.Literal("a"), S.String)) + +// @ts-expect-error +S.Config("A", S.Boolean) diff --git a/packages/effect/src/Schema.ts b/packages/effect/src/Schema.ts index 88376f6afaa..b5b8bb06c52 100644 --- a/packages/effect/src/Schema.ts +++ b/packages/effect/src/Schema.ts @@ -9393,11 +9393,11 @@ export class BooleanFromString extends transform( * @category Config validations * @since 3.10.0 */ -export const Config = (name: string, schema: Schema): config_.Config => { +export const Config = (name: string, schema: Schema): config_.Config => { const decodeEither_ = decodeEither(schema) return config_.string(name).pipe( config_.mapOrFail((a) => - decodeEither_(a).pipe( + decodeEither_(a as B).pipe( either_.mapLeft((error) => configError_.InvalidData([], ParseResult.TreeFormatter.formatErrorSync(error))) ) ) diff --git a/packages/effect/test/Schema/Schema/Config/Config.test.ts b/packages/effect/test/Schema/Schema/Config/Config.test.ts index 4196525f796..13ec9edf107 100644 --- a/packages/effect/test/Schema/Schema/Config/Config.test.ts +++ b/packages/effect/test/Schema/Schema/Config/Config.test.ts @@ -56,4 +56,17 @@ describe("Config", () => { ) ) }) + + it("should work with a template literal", () => { + const config = Schema.Config("A", Schema.TemplateLiteral("a", Schema.Number)) + assertSuccess(config, [["A", "a1"]], "a1") + assertFailure( + config, + [["A", "ab"]], + ConfigError.InvalidData( + ["A"], + `Expected \`a$\{number}\`, actual "ab"` + ) + ) + }) }) From ae8310254e529887f6fe73124577c9d3dda56301 Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Wed, 18 Dec 2024 09:12:25 +0000 Subject: [PATCH 2/3] Make it a patch-level change --- .changeset/good-pigs-roll.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/good-pigs-roll.md b/.changeset/good-pigs-roll.md index 332d6e23628..21f9021e263 100644 --- a/.changeset/good-pigs-roll.md +++ b/.changeset/good-pigs-roll.md @@ -1,5 +1,5 @@ --- -"effect": minor +"effect": patch --- Support template literals in Schema.Config From 2788bcd1415879c63eabca1b22ebfd01599e7b0b Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Wed, 18 Dec 2024 09:17:12 +0000 Subject: [PATCH 3/3] Match review --- packages/effect/src/Schema.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/effect/src/Schema.ts b/packages/effect/src/Schema.ts index b5b8bb06c52..c62a37c4e00 100644 --- a/packages/effect/src/Schema.ts +++ b/packages/effect/src/Schema.ts @@ -9393,12 +9393,12 @@ export class BooleanFromString extends transform( * @category Config validations * @since 3.10.0 */ -export const Config = (name: string, schema: Schema): config_.Config => { - const decodeEither_ = decodeEither(schema) +export const Config = (name: string, schema: Schema): config_.Config => { + const decodeUnknownEither = ParseResult.decodeUnknownEither(schema) return config_.string(name).pipe( - config_.mapOrFail((a) => - decodeEither_(a as B).pipe( - either_.mapLeft((error) => configError_.InvalidData([], ParseResult.TreeFormatter.formatErrorSync(error))) + config_.mapOrFail((s) => + decodeUnknownEither(s).pipe( + either_.mapLeft((error) => configError_.InvalidData([], ParseResult.TreeFormatter.formatIssueSync(error))) ) ) )