From 49d4273b3c1b23fca68e9600cdb2cc0c6d17e438 Mon Sep 17 00:00:00 2001 From: Titouan CREACH Date: Thu, 29 Aug 2024 12:02:08 +0200 Subject: [PATCH 1/5] add a nonEmptyString constructor to Config --- packages/effect/src/Config.ts | 8 ++++++++ packages/effect/src/internal/config.ts | 10 ++++++++++ packages/effect/test/Config.test.ts | 19 +++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/packages/effect/src/Config.ts b/packages/effect/src/Config.ts index 294157446c..bb6ef8f85c 100644 --- a/packages/effect/src/Config.ts +++ b/packages/effect/src/Config.ts @@ -367,6 +367,14 @@ export const hashSet: (config: Config, name?: string) => Config Config = internal.string +/** + * Constructs a config for a non-empty string value. + * + * @since ?? + * @category constructors + */ +export const nonEmptyString: (name?: string) => Config = internal.nonEmptyString + /** * Constructs a config which contains the specified value. * diff --git a/packages/effect/src/internal/config.ts b/packages/effect/src/internal/config.ts index d3125aa1c2..9bc9930fe5 100644 --- a/packages/effect/src/internal/config.ts +++ b/packages/effect/src/internal/config.ts @@ -447,6 +447,16 @@ export const string = (name?: string): Config.Config => { return name === undefined ? config : nested(config, name) } +/** @internal */ +export const nonEmptyString = (name?: string): Config.Config => { + const config = primitive( + "a non-empty text property", + Either.liftPredicate((text) => text.length > 0, () => configError.InvalidData([], "Expected a non-empty string")) + ) + + return name === undefined ? config : nested(config, name) +} + /** @internal */ export const all = > | Record>>( arg: Arg diff --git a/packages/effect/test/Config.test.ts b/packages/effect/test/Config.test.ts index 00ec1ecc4c..3e0f397af6 100644 --- a/packages/effect/test/Config.test.ts +++ b/packages/effect/test/Config.test.ts @@ -66,6 +66,25 @@ describe("Config", () => { }) }) + describe("nonEmptyString", () => { + it("name = undefined", () => { + const config = Config.array(Config.nonEmptyString(), "ITEMS") + assertSuccess(config, [["ITEMS", "foo"]], ["foo"]) + assertFailure(config, [["ITEMS", ""]], ConfigError.InvalidData(["ITEMS"], "Expected a non-empty string")) + }) + + it("name != undefined", () => { + const config = Config.nonEmptyString("NON_EMPTY_STRING") + assertSuccess(config, [["NON_EMPTY_STRING", "foo"]], "foo") + assertSuccess(config, [["NON_EMPTY_STRING", " "]], " ") + assertFailure( + config, + [["NON_EMPTY_STRING", ""]], + ConfigError.InvalidData(["NON_EMPTY_STRING"], "Expected a non-empty string") + ) + }) + }) + describe("number", () => { it("name = undefined", () => { const config = Config.array(Config.number(), "ITEMS") From 50ecf3a4a6dc37bbf8264432eb8ec0830a5515f0 Mon Sep 17 00:00:00 2001 From: Titouan CREACH Date: Thu, 29 Aug 2024 12:02:22 +0200 Subject: [PATCH 2/5] Target next minor --- packages/effect/src/Config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/effect/src/Config.ts b/packages/effect/src/Config.ts index bb6ef8f85c..04f82fcccd 100644 --- a/packages/effect/src/Config.ts +++ b/packages/effect/src/Config.ts @@ -370,7 +370,7 @@ export const string: (name?: string) => Config = internal.string /** * Constructs a config for a non-empty string value. * - * @since ?? + * @since 3.7.0 * @category constructors */ export const nonEmptyString: (name?: string) => Config = internal.nonEmptyString From 17e94c3081bc016d921cb0eb1aacb11fb209d18f Mon Sep 17 00:00:00 2001 From: Titouan CREACH Date: Thu, 29 Aug 2024 12:08:14 +0200 Subject: [PATCH 3/5] add a changeset --- .changeset/hot-dogs-mix.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/hot-dogs-mix.md diff --git a/.changeset/hot-dogs-mix.md b/.changeset/hot-dogs-mix.md new file mode 100644 index 0000000000..c9a0549355 --- /dev/null +++ b/.changeset/hot-dogs-mix.md @@ -0,0 +1,5 @@ +--- +"effect": minor +--- + +New constructor Config.nonEmptyString From bdd8368beaea0731b1dbf8a77501652f905b1942 Mon Sep 17 00:00:00 2001 From: Titouan CREACH Date: Fri, 30 Aug 2024 05:00:46 +0200 Subject: [PATCH 4/5] Rename nonEmptyString to stringNonEmpty --- .changeset/hot-dogs-mix.md | 2 +- packages/effect/src/Config.ts | 2 +- packages/effect/src/internal/config.ts | 2 +- packages/effect/test/Config.test.ts | 6 +++--- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.changeset/hot-dogs-mix.md b/.changeset/hot-dogs-mix.md index c9a0549355..ddcdf696df 100644 --- a/.changeset/hot-dogs-mix.md +++ b/.changeset/hot-dogs-mix.md @@ -2,4 +2,4 @@ "effect": minor --- -New constructor Config.nonEmptyString +New constructor Config.stringNonEmpty diff --git a/packages/effect/src/Config.ts b/packages/effect/src/Config.ts index 04f82fcccd..221ee024e6 100644 --- a/packages/effect/src/Config.ts +++ b/packages/effect/src/Config.ts @@ -373,7 +373,7 @@ export const string: (name?: string) => Config = internal.string * @since 3.7.0 * @category constructors */ -export const nonEmptyString: (name?: string) => Config = internal.nonEmptyString +export const stringNonEmpty: (name?: string) => Config = internal.stringNonEmpty /** * Constructs a config which contains the specified value. diff --git a/packages/effect/src/internal/config.ts b/packages/effect/src/internal/config.ts index 9bc9930fe5..a0cfe112bf 100644 --- a/packages/effect/src/internal/config.ts +++ b/packages/effect/src/internal/config.ts @@ -448,7 +448,7 @@ export const string = (name?: string): Config.Config => { } /** @internal */ -export const nonEmptyString = (name?: string): Config.Config => { +export const stringNonEmpty = (name?: string): Config.Config => { const config = primitive( "a non-empty text property", Either.liftPredicate((text) => text.length > 0, () => configError.InvalidData([], "Expected a non-empty string")) diff --git a/packages/effect/test/Config.test.ts b/packages/effect/test/Config.test.ts index 3e0f397af6..a1ac52fa45 100644 --- a/packages/effect/test/Config.test.ts +++ b/packages/effect/test/Config.test.ts @@ -66,15 +66,15 @@ describe("Config", () => { }) }) - describe("nonEmptyString", () => { + describe("stringNonEmpty", () => { it("name = undefined", () => { - const config = Config.array(Config.nonEmptyString(), "ITEMS") + const config = Config.array(Config.stringNonEmpty(), "ITEMS") assertSuccess(config, [["ITEMS", "foo"]], ["foo"]) assertFailure(config, [["ITEMS", ""]], ConfigError.InvalidData(["ITEMS"], "Expected a non-empty string")) }) it("name != undefined", () => { - const config = Config.nonEmptyString("NON_EMPTY_STRING") + const config = Config.stringNonEmpty("NON_EMPTY_STRING") assertSuccess(config, [["NON_EMPTY_STRING", "foo"]], "foo") assertSuccess(config, [["NON_EMPTY_STRING", " "]], " ") assertFailure( From 8f0cad30499a17317dbb87c5e1375c59ee21f9e7 Mon Sep 17 00:00:00 2001 From: Titouan CREACH Date: Fri, 30 Aug 2024 05:06:14 +0200 Subject: [PATCH 5/5] consider empty string as MissingData error --- packages/effect/src/internal/config.ts | 2 +- packages/effect/test/Config.test.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/effect/src/internal/config.ts b/packages/effect/src/internal/config.ts index a0cfe112bf..44e805ec5f 100644 --- a/packages/effect/src/internal/config.ts +++ b/packages/effect/src/internal/config.ts @@ -451,7 +451,7 @@ export const string = (name?: string): Config.Config => { export const stringNonEmpty = (name?: string): Config.Config => { const config = primitive( "a non-empty text property", - Either.liftPredicate((text) => text.length > 0, () => configError.InvalidData([], "Expected a non-empty string")) + Either.liftPredicate((text) => text.length > 0, () => configError.MissingData([], "Expected a non-empty string")) ) return name === undefined ? config : nested(config, name) diff --git a/packages/effect/test/Config.test.ts b/packages/effect/test/Config.test.ts index a1ac52fa45..a96a8cf191 100644 --- a/packages/effect/test/Config.test.ts +++ b/packages/effect/test/Config.test.ts @@ -70,7 +70,7 @@ describe("Config", () => { it("name = undefined", () => { const config = Config.array(Config.stringNonEmpty(), "ITEMS") assertSuccess(config, [["ITEMS", "foo"]], ["foo"]) - assertFailure(config, [["ITEMS", ""]], ConfigError.InvalidData(["ITEMS"], "Expected a non-empty string")) + assertFailure(config, [["ITEMS", ""]], ConfigError.MissingData(["ITEMS"], "Expected a non-empty string")) }) it("name != undefined", () => { @@ -80,7 +80,7 @@ describe("Config", () => { assertFailure( config, [["NON_EMPTY_STRING", ""]], - ConfigError.InvalidData(["NON_EMPTY_STRING"], "Expected a non-empty string") + ConfigError.MissingData(["NON_EMPTY_STRING"], "Expected a non-empty string") ) }) })