gcanti / io-ts-types

A collection of codecs and combinators for use with io-ts
https://gcanti.github.io/io-ts-types/
MIT License
311 stars 40 forks source link

`JSONFromString` #153

Closed OliverJAsh closed 3 years ago

OliverJAsh commented 3 years ago

🚀 Feature request

This package used to provide a JSONFromString type but it looks like this was removed in v0.5.0. Perhaps this was removed in favour of Either.parseJSON?

However, it would still be useful to provide a JSONFromString type in this package because it's not easy to define yourself using Either.parseJSON.

export const JSONFromString = new t.Type<E.Json, string, string>(
  'JSONFromString',
  // TODO:
  (x): x is E.Json => true,
  (s, c) =>
    pipe(
      E.parseJSON(s, (_e) => 'whatever'),
      E.fold(() => t.failure(s, c), t.success),
    ),
  JSON.stringify,
);

In order to correctly implement the is function we would need to create a Json runtime type:

Would you consider a PR that brings this back?