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

Release queue: minor #3410

Merged
merged 12 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add a Config constructor : Config.stringNonEmpty (#3514)
  • Loading branch information
titouancreach authored and tim-smart committed Aug 30, 2024
commit 08c888497c1181d326e8e7e9236fb969b3c12bf1
5 changes: 5 additions & 0 deletions .changeset/hot-dogs-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 1,5 @@
---
"effect": minor
---

New constructor Config.stringNonEmpty
8 changes: 8 additions & 0 deletions packages/effect/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 367,14 @@ export const hashSet: <A>(config: Config<A>, name?: string) => Config<HashSet.Ha
*/
export const string: (name?: string) => Config<string> = internal.string

/**
* Constructs a config for a non-empty string value.
*
* @since 3.7.0
* @category constructors
*/
export const stringNonEmpty: (name?: string) => Config<string> = internal.stringNonEmpty

/**
* Constructs a config which contains the specified value.
*
Expand Down
10 changes: 10 additions & 0 deletions packages/effect/src/internal/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 447,16 @@ export const string = (name?: string): Config.Config<string> => {
return name === undefined ? config : nested(config, name)
}

/** @internal */
export const stringNonEmpty = (name?: string): Config.Config<string> => {
const config = primitive(
"a non-empty text property",
Either.liftPredicate((text) => text.length > 0, () => configError.MissingData([], "Expected a non-empty string"))
)

return name === undefined ? config : nested(config, name)
}

/** @internal */
export const all = <const Arg extends Iterable<Config.Config<any>> | Record<string, Config.Config<any>>>(
arg: Arg
Expand Down
19 changes: 19 additions & 0 deletions packages/effect/test/Config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 66,25 @@ describe("Config", () => {
})
})

describe("stringNonEmpty", () => {
it("name = undefined", () => {
const config = Config.array(Config.stringNonEmpty(), "ITEMS")
assertSuccess(config, [["ITEMS", "foo"]], ["foo"])
assertFailure(config, [["ITEMS", ""]], ConfigError.MissingData(["ITEMS"], "Expected a non-empty string"))
})

it("name != undefined", () => {
const config = Config.stringNonEmpty("NON_EMPTY_STRING")
assertSuccess(config, [["NON_EMPTY_STRING", "foo"]], "foo")
assertSuccess(config, [["NON_EMPTY_STRING", " "]], " ")
assertFailure(
config,
[["NON_EMPTY_STRING", ""]],
ConfigError.MissingData(["NON_EMPTY_STRING"], "Expected a non-empty string")
)
})
})

describe("number", () => {
it("name = undefined", () => {
const config = Config.array(Config.number(), "ITEMS")
Expand Down