diff --git a/.changeset/slimy-swans-repeat.md b/.changeset/slimy-swans-repeat.md new file mode 100644 index 00000000000..ca5c5ec3fd1 --- /dev/null +++ b/.changeset/slimy-swans-repeat.md @@ -0,0 +1,5 @@ +--- +"effect": patch +--- + +Fix use of Schema.NonEmptyArrayEnsure with strings diff --git a/packages/effect/src/Schema.ts b/packages/effect/src/Schema.ts index 08719a018d5..fb52d76177e 100644 --- a/packages/effect/src/Schema.ts +++ b/packages/effect/src/Schema.ts @@ -1664,7 +1664,7 @@ export interface NonEmptyArrayEnsure export function NonEmptyArrayEnsure(value: Value): NonEmptyArrayEnsure { return transform(Union(value, NonEmptyArray(value)), NonEmptyArray(typeSchema(asSchema(value))), { strict: true, - decode: (i) => array_.isNonEmptyReadonlyArray(i) ? i : array_.of(i), + decode: (i) => Array.isArray(i) && array_.isNonEmptyReadonlyArray(i) ? i : (array_.of(i) as never), encode: (a) => a.length === 1 ? a[0] : a }) } diff --git a/packages/effect/test/Schema/Schema/NonEmptyArrayEnsure.test.ts b/packages/effect/test/Schema/Schema/NonEmptyArrayEnsure.test.ts index ec26ef4e68c..b5cb27f0630 100644 --- a/packages/effect/test/Schema/Schema/NonEmptyArrayEnsure.test.ts +++ b/packages/effect/test/Schema/Schema/NonEmptyArrayEnsure.test.ts @@ -5,7 +5,9 @@ import * as Util from "../TestUtils.js" describe("NonEmptyArrayEnsure", () => { it("decode non-array", async () => { const schema = S.NonEmptyArrayEnsure(S.NumberFromString) + const schema2 = S.NonEmptyArrayEnsure(S.String) await Util.assertions.decoding.succeed(schema, "123", [123]) + await Util.assertions.decoding.succeed(schema2, "foo", ["foo"]) await Util.assertions.decoding.fail( schema, null, @@ -56,6 +58,11 @@ describe("NonEmptyArrayEnsure", () => { ) }) + it("decode array value", async () => { + const schema = S.NonEmptyArrayEnsure(S.Array(S.String)) + await Util.assertions.decoding.succeed(schema, [], [[]]) + }) + it("encode", async () => { const schema = S.NonEmptyArrayEnsure(S.NumberFromString) await Util.assertions.encoding.succeed(schema, [123], "123")