Created
June 25, 2017 01:23
-
-
Save kofno/974dc4f9897c4646d938c6f156130ca7 to your computer and use it in GitHub Desktop.
The maybe decoder
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 { maybe, string, field } from 'jsonous'; | |
| const decoder = field('foo', maybe(string())); | |
| decoder.decodeAny({ foo: 'bar' }) | |
| // --> Ok(Just('bar')) | |
| decoder.decodeAny({ foo: null }) | |
| // --> Ok(Nothing) | |
| decoder.decodeAny({ foop: 'bar' }) | |
| // --> Err('Expected to find an object with key 'foo'. Instead found {"foop":"bar"}') | |
| const maybeDecoder = maybe(field('foo', string())); | |
| maybeDecoder.decodeAny({ foop: 'bar' }) | |
| // --> Ok(Nothing) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment