io-ts icon indicating copy to clipboard operation
io-ts copied to clipboard

Type Errors and Context as NonEmptyArray

Open devkat opened this issue 3 years ago • 0 comments
trafficstars

🚀 Feature request

Current Behavior

Errors and Context are typed as Array:

export interface Context extends ReadonlyArray<ContextEntry> {}
export interface Errors extends Array<ValidationError> {}
export type Validation<A> = Either<Errors, A>

Desired Behavior

Errors should be typed as NonEmptyArray, since the absence of errors would constitute a success (either.Right):

export interface Errors extends NonEmptyArray<ValidationError> {}
export type Validation<A> = Either<Errors, A>

See also https://zio.github.io/zio-prelude/docs/functionaldatatypes/validation

Context should be typed as ReadonlyNonEmptyArray since at least one context should exist for an error:

export interface Context extends ReadonlyNonEmptyArray<ContextEntry> {}

Suggested Solution

See pull request

Who does this impact? Who is this for?

The solution simplifies error handling since the user can rely on the fact that at least one error with at least one context is present.

Describe alternatives you've considered

Additional context

Your environment

Software Version(s)
io-ts 2.2.19
fp-ts 2.9.5
TypeScript 4.8.2

devkat avatar Nov 02 '22 09:11 devkat