Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a reporter with suggestions #37

Open
gillchristian opened this issue Jun 25, 2020 · 1 comment
Open

Implement a reporter with suggestions #37

gillchristian opened this issue Jun 25, 2020 · 1 comment

Comments

@gillchristian
Copy link
Owner

Current reporter

import * as t from 'io-ts';
import Reporter from 'io-ts-reporters';

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

Reporter.report(User.decode({ nam: 'Jane' })); 

The reported error from that example would be:

Expecting string at name but instead got: undefined

This is good for usage on an error aggregator (eg. honeybadger.io) where you just want to know what went wrong.

An smarter reporter

But there could be an use case for an smarter reporter that would also give suggestions.

Specially because, although the reporter error on the example is correct by saying name property was not provided. It is also obvious (to a human) that it wasn't provided because there's a typo (ie. the object does have a very similar property that even matches the expected type of the value).

A good inspiration could be Elm's error messages.

For the following code

type alias Model = { name : String, password : String }

init : Model
init = { nam = "", password = "" }

The error message is:

-- TYPE MISMATCH ----------------------------------------------- Jump To Problem

Something is off with the body of the `init` definition:

33|   { nam = "", password = "" }
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^
The body is a record of type:

    { nam : String, password : String }

But the type annotation on `init` says it should be:

    Model

Hint: Seems like a record field typo. Maybe nam should be name?

Hint: Can more type annotations be added? Type annotations always help me give
more specific messages, and I think they could help a lot in this case!

About use cases

Since io-ts is meant for runtime validation I am not sure if this is something that would be used as much as the "regular" reporter.

@gillchristian
Copy link
Owner Author

TypeScript does provide some suggestions in the errors as well:

type User = { name: string }

const jane: User = { nam: 'Jane' }
Type '{ nam: string; }' is not assignable to type 'User'.
  Object literal may only specify known properties, but 'nam' does not exist in type 'User'. Did you mean to write 'name'?

See on playground: tsplay.dev/lWYJ2w

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant