Skip to content

2.0 • Raw value decoding

Latest

Choose a tag to compare

@KyNorthstar KyNorthstar released this 23 Dec 05:32
89bdbb8

Breaking Changes

While this update preserves existing decoding paths while adding an additional path, it does remove an error which might have previously been thrown.

In case any users of this library depended on that error to validate encoded data which doesn't match their desired format, this change is considered breaking and requires a MAJOR version bump.

Summary

Now Either can be used to decode payloads which encode different types in the same field, without wrapping those fields in "left"/"right" sub-objects.

The decoder now attempts to interpret a single‑value container as the left type, and if that fails, the right type. Of course, if both fail to decode, then a NeitherLeftNorRightValueWasEncoded error is thrown just as always.

Either<String, Int>
{ "value": "foo" }          // decodes to either.left("foo")
{ "value": 42 }             // decodes to either.right(42)

Usage notes

Since this only depends on which types the field can encode as, this will always decode as .left when the left and right types of the Either are the same:

Either<String, String>
{ "value": "foo" }          // decodes to either.left("foo")
{ "value": "baz" }          // decodes to either.left("baz")