Skip to content

Commit

Permalink
Support template literals in Schema.Config (#4113)
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilkybarkid authored Dec 18, 2024
1 parent e151ad3 commit 1c08a0b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-pigs-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Support template literals in Schema.Config
16 changes: 16 additions & 0 deletions packages/effect/dtslint/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2922,3 +2922,19 @@ S.NullOr(S.Never)

// $ExpectType NullishOr<typeof Never>
S.NullishOr(S.Never)

// ---------------------------------------------
// Config
// ---------------------------------------------

// $ExpectType Config<string>
S.Config("A", S.String)

// $ExpectType Config<boolean>
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)
10 changes: 5 additions & 5 deletions packages/effect/src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9495,12 +9495,12 @@ export class BooleanFromString extends transform(
* @category Config validations
* @since 3.10.0
*/
export const Config = <A>(name: string, schema: Schema<A, string>): config_.Config<A> => {
const decodeEither_ = decodeEither(schema)
export const Config = <A, I extends string>(name: string, schema: Schema<A, I>): config_.Config<A> => {
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)))
)
)
)
Expand Down
13 changes: 13 additions & 0 deletions packages/effect/test/Schema/Schema/Config/Config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
)
)
})
})

0 comments on commit 1c08a0b

Please sign in to comment.