Skip to content

Commit

Permalink
Add a Config constructor : Config.stringNonEmpty (#3514)
Browse files Browse the repository at this point in the history
  • Loading branch information
titouancreach authored Aug 30, 2024
1 parent 0045357 commit de32b32
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
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

0 comments on commit de32b32

Please sign in to comment.