io-ts-reporters

Formatting of io-ts validation errors

MIT 27 个版本
安装
npm install io-ts-reporters
yarn add io-ts-reporters
pnpm add io-ts-reporters
bun add io-ts-reporters
README

io-ts-reporters

Error reporters for io-ts.

scarf-downloads

Currently this package only includes one reporter. The output is an array of strings in the format of:

Expecting ${expectedType} at ${path} but instead got: ${actualValue}

And for union types:

Expecting one of:
    ${unionType1}
    ${unionType2}
    ${...}
    ${unionTypeN}
at ${path} but instead got: ${actualValue}

Installation

yarn add io-ts-reporters

Example

import * as t from 'io-ts'
import reporter from 'io-ts-reporters'

const User = t.interface({name: t.string})

// When decoding fails, the errors are reported
reporter.report(User.decode({nam: 'Jane'}))
//=> ['Expecting string at name but instead got: undefined']

// Nothing gets reported on success
reporter.report(User.decode({name: 'Jane'}))
//=> []

To only format the validation errors in case the validation failed (ie. mapLeft) use formatValidationErrors instead.

import * as t from 'io-ts'
import {formatValidationErrors} from 'io-ts-reporters'
import * as E from 'fp-ts/Either'
import {pipe} from 'fp-ts/pipeable'

const User = t.interface({name: t.string})

const result = User.decode({nam: 'Jane'}) // Either<t.Errors, User>

E.mapLeft(formatValidationErrors)(result) // Either<string[], User>

For more examples see the tests.

TypeScript compatibility

io-ts-reporters version required typescript version
1.0.0 3.5+
<= 0.0.21 2.7+

Testing

yarn
yarn run test

Credits

This library was created by OliverJAsh.

版本列表
2.0.1 2022-02-07
2.0.0 2021-07-14
2.0.0-rc1 2021-07-14
1.2.2 2020-07-21
1.2.1 2020-07-15
1.2.0 2020-06-25
1.1.0 2020-06-10
1.0.0 2019-07-26
0.0.21 2018-08-11
0.0.20 2018-02-17
0.0.19 2018-02-17
0.0.18 2018-01-29
0.0.17 2017-11-10
0.0.16 2017-09-23
0.0.15 2017-07-28
0.0.14 2017-07-14
0.0.13 2017-07-14
0.0.12 2017-06-09
0.0.11 2017-06-09
0.0.9 2017-04-19
0.0.8 2017-04-19
0.0.7 2017-04-19
0.0.6 2017-04-19
0.0.5 2017-04-19
0.0.4 2017-04-19
0.0.3 2017-04-19
0.0.2 2017-04-19