gcanti / io-ts

Runtime type system for IO decoding/encoding
https://gcanti.github.io/io-ts/
MIT License
6.68k stars 331 forks source link

Type Errors and Context as NonEmptyArray #670

Open devkat opened 1 year ago

devkat commented 1 year ago

πŸš€ 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