-
-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@effect/schema": patch | ||
--- | ||
|
||
Add support for `Config` module, closes #2346 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import * as Schema from "@effect/schema/Schema" | ||
import type * as Config from "effect/Config" | ||
import * as ConfigError from "effect/ConfigError" | ||
import * as ConfigProvider from "effect/ConfigProvider" | ||
import * as Effect from "effect/Effect" | ||
import * as Exit from "effect/Exit" | ||
import { describe, expect, it } from "vitest" | ||
|
||
/** | ||
* Asserts that loading a configuration with invalid data fails with the expected error. | ||
* | ||
* @param config - The configuration to load. | ||
* @param map - The map of configuration values. | ||
* @param error - The expected error. | ||
*/ | ||
const assertFailure = <A>( | ||
config: Config.Config<A>, | ||
map: ReadonlyArray<readonly [string, string]>, | ||
error: ConfigError.ConfigError | ||
) => { | ||
const configProvider = ConfigProvider.fromMap(new Map(map)) | ||
const result = Effect.runSync(Effect.exit(configProvider.load(config))) | ||
expect(result).toStrictEqual(Exit.fail(error)) | ||
} | ||
|
||
/** | ||
* Asserts that loading a configuration with valid data succeeds and returns the expected value. | ||
* | ||
* @param config - The configuration to load. | ||
* @param map - The map of configuration values. | ||
* @param a - The expected value. | ||
*/ | ||
const assertSuccess = <A>( | ||
config: Config.Config<A>, | ||
map: ReadonlyArray<readonly [string, string]>, | ||
a: A | ||
) => { | ||
const configProvider = ConfigProvider.fromMap(new Map(map)) | ||
const result = Effect.runSync(Effect.exit(configProvider.load(config))) | ||
expect(result).toStrictEqual(Exit.succeed(a)) | ||
} | ||
|
||
describe("Config", () => { | ||
it("should validate the configuration schema correctly", () => { | ||
const config = Schema.Config("A", Schema.NonEmpty) | ||
assertSuccess(config, [["A", "a"]], "a") | ||
assertFailure(config, [], ConfigError.MissingData(["A"], `Expected A to exist in the provided map`)) | ||
assertFailure( | ||
config, | ||
[["A", ""]], | ||
ConfigError.InvalidData( | ||
["A"], | ||
`NonEmpty | ||
└─ Predicate refinement failure | ||
└─ Expected NonEmpty (a non empty string), actual ""` | ||
) | ||
) | ||
}) | ||
}) |