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

Support template literals in Schema.Config #4113

Merged
merged 3 commits into from
Dec 18, 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/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 @@ -2895,3 +2895,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 @@ -9393,12 +9393,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"`
)
)
})
})
Loading