Skip to content

Instantly share code, notes, and snippets.

@kofno
Created June 25, 2017 01:23
Show Gist options
  • Select an option

  • Save kofno/974dc4f9897c4646d938c6f156130ca7 to your computer and use it in GitHub Desktop.

Select an option

Save kofno/974dc4f9897c4646d938c6f156130ca7 to your computer and use it in GitHub Desktop.
The maybe decoder
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