Coder Social home page Coder Social logo

io-ts-structural-error-reporter's Introduction

Structural io-ts Error Reporter

Library to give you structural error reports from validation errors created by io-ts. The library is motivated by a desire to use io-ts to validate a REST API's JSON input and return errors in an approachable format.

Installation

npm install --save-exact @pavelkucera/io-ts-structural-error-reporter

Usage

Use StructuralErrorReporter.report to convert result of Type.decode into errors copying the underlying Type structure. The type of the report function is:

import { Either } from 'fp-ts/lib/Either'
import * as t from 'io-ts'

type Reporter = (validation: t.Validation<any>) => Either<InternalError, ErrorReport>

type ErrorReportRecord = {
  [key: string]: ErrorReport
}

type ErrorReport =
  | string
  | ErrorReportRecord

The reporter is designed not to throw any errors during runtime, which is why the return value is of type Either. In general, an error/Left result means that a "this should never happen" error happened in library or on the boundary between the library and io-ts. In such a case, the only course of action should be aborting (and reporting an issue).

The reporter is supposed to run only when a validation fails, and thus the reporter reports an error when validation succeeds. Now on to the example that you are waiting for!

Example

import { InternalError, StructuralErrorReporter } from '@pavelkucera/io-ts-structural-error-reporter'
import * as Either from 'fp-ts/lib/Either'
import * as t from 'io-ts'

const type = t.type({
  string: t.string,
  nested: t.type({
    property: t.string,
  })
})

const decodedValue = type.decode({
  string: 42, // should be string
  nested: {
    property: 42, // should be string
  }
});

const errorReport = StructuralErrorReporter.report(decodedValue);

// Use some of the Either functionality to unwrap the result.
const structrualErrorReport = Either.getOrElse<InternalError, ErrorReport>(
  _error => { throw new Error('Panic') }
)(errorReport)

console.log(structrualErrorReport);
// prints:
// {
//   string: 'Expecting "string".',
//   nested: {
//     property: 'Expecting "string".',
//   },
// }

Error Messages

If the codec defines its own error message, the library will use that error message. Otherwise, a default error message will be used. The default error message is in format:

Expecting: "<name of the codec>".

io-ts-structural-error-reporter's People

Contributors

dependabot[bot] avatar pavelkucera avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

io-ts-structural-error-reporter's Issues

Reconsider arrays structure

Currently arrays will be encoded as

{
  '0': '0',
  '1': '1',
}

but it might useful to encode them as arrays

['0', '1']

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.