diff --git a/.changeset/good-pigs-roll.md b/.changeset/good-pigs-roll.md new file mode 100644 index 00000000000..21f9021e263 --- /dev/null +++ b/.changeset/good-pigs-roll.md @@ -0,0 +1,5 @@ +--- +"effect": patch +--- + +Support template literals in Schema.Config diff --git a/packages/effect/dtslint/Schema.ts b/packages/effect/dtslint/Schema.ts index 19d387ce2b6..9605d4cdab9 100644 --- a/packages/effect/dtslint/Schema.ts +++ b/packages/effect/dtslint/Schema.ts @@ -2922,3 +2922,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 89cb8071dc2..7fea7310485 100644 --- a/packages/effect/src/Schema.ts +++ b/packages/effect/src/Schema.ts @@ -9495,12 +9495,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).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))) ) ) ) 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"` + ) + ) + }) })