https://github.com/gillchristian/io-ts-reporters
Error reporters for io-ts
https://github.com/gillchristian/io-ts-reporters
fp-ts io-ts type-safety validation
Last synced: about 1 month ago
JSON representation
Error reporters for io-ts
- Host: GitHub
- URL: https://github.com/gillchristian/io-ts-reporters
- Owner: gillchristian
- License: mit
- Created: 2017-04-19T08:23:56.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2024-06-10T13:18:48.000Z (11 months ago)
- Last Synced: 2025-03-29T01:14:56.055Z (about 2 months ago)
- Topics: fp-ts, io-ts, type-safety, validation
- Language: TypeScript
- Homepage: https://gillchristian.github.io/io-ts-reporters
- Size: 691 KB
- Stars: 89
- Watchers: 1
- Forks: 17
- Open Issues: 24
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-fp-ts - gillchristian/io-ts-reporters - Error reporters (Domain modeling in TypeScript by Benoit Ruiz)
README
# io-ts-reporters
[Error reporters](https://github.com/gcanti/io-ts#error-reporters) for
[io-ts](https://github.com/gcanti/io-ts).
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
```bash
yarn add io-ts-reporters
```## Example
```ts
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.```ts
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
E.mapLeft(formatValidationErrors)(result) // Either
```For more examples see [the tests](./tests/index.test.ts).
## TypeScript compatibility
| io-ts-reporters version | required typescript version |
| ----------------------- | --------------------------- |
| 1.0.0 | 3.5+ |
| <= 0.0.21 | 2.7+ |## Testing
```bash
yarn
yarn run test
```[io-ts]: https://github.com/gcanti/io-ts#error-reporters
## Credits
This library was created by [OliverJAsh](https://github.com/OliverJAsh).