Skip to content

Instantly share code, notes, and snippets.

@kofno
Last active June 25, 2017 17:30
Show Gist options
  • Select an option

  • Save kofno/214a66cf4de546f6fb53649b5dece4e7 to your computer and use it in GitHub Desktop.

Select an option

Save kofno/214a66cf4de546f6fb53649b5dece4e7 to your computer and use it in GitHub Desktop.
andThen applied to decoders
import { field, string, boolean, number, succeed } from 'jsonous';
const decoder =
field('foo', string()).map(s => s + '-ish').andThen(foo =>
field('bar', number()).map(n => n*2).andThen(bar =>
field('baz', boolean()).map(b => !b).andThen(baz =>
succeed({ foo, bar, baz }))));
decoder.decodeAny({ foo: 'hello', bar: 42, baz: true });
// --> Ok({ foo: 'hello-ish', bar: 84, baz: false })
decoder.decodeAny({ foo: 'hello', bar: 'fourty-two', baz: true });
// --> Err('Error found in field 'bar' of {"foo":"hello","bar":"fourty-two","baz":true}: Expected to find a number. Instead found "fourty-two"');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment