Last active
June 25, 2017 17:30
-
-
Save kofno/214a66cf4de546f6fb53649b5dece4e7 to your computer and use it in GitHub Desktop.
andThen applied to decoders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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