Fix use of Schema.NonEmptyArrayEnsure with strings - #7170
Conversation
🦋 Changeset detectedLatest commit: 37f89d6 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
`Schema.NonEmptyArrayEnsure` exists, but doesn't work with strings (Effect-TS/effect#7170)
|
Run failed. View the logs →
|
gcanti
left a comment
There was a problem hiding this comment.
Array.ensure changes the existing behavior when the value schema itself accepts arrays. For example, an empty array is a valid single value here:
const schema = S.NonEmptyArrayEnsure(S.Array(S.String))On v3, decoding [] produces [[]]. With Array.ensure, it remains [] and is then rejected by the target NonEmptyArray schema.
Could we preserve that behavior while also checking that non-empty values are actual arrays?
decode: (i) =>
(Array.isArray(i) && array_.isNonEmptyReadonlyArray(i) ? i : array_.of(i)),I would also add this regression test:
it("decode array value", async () => {
const schema = S.NonEmptyArrayEnsure(S.Array(S.String))
await Util.assertions.decoding.succeed(schema, [], [[]])
})This should cover both the string fix and the previous empty-array behavior.

Type
Description
Schema.NonEmptyArrayEnsuredoesn't work with strings. The compiler doesn't recognise that the value being passed toArray.isNonEmptyReadonlyArraymight not be an array, and the function only checks thelengthproperty which also exists on strings.Related