Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined values are decoded with the specified default value #15

Merged
merged 3 commits into from
Apr 6, 2021

Conversation

ilario-pierbattista
Copy link
Member

@ilario-pierbattista ilario-pierbattista commented Apr 5, 2021

The usage should be like:

Codecs::classFromArray(
  [
    'foo' => Codecs::string(),
    'bar' => Codecs::union(Codecs::int(), Codecs::undefined(-1))
  ],
  ...
);

Resolves #6

@ilario-pierbattista ilario-pierbattista marked this pull request as ready for review April 6, 2021 07:05
@ilario-pierbattista
Copy link
Member Author

The undefined codec has strange behavior.

In PHP, there is nothing equivalent to the undefined of javascript.
To simulate it I rely on an internal class named Undefined.
The codec will receive a default value by construction, which will substitute this class, as I don't want to expose it.

The encoding process should reverse transform the default value to an instance of the internal class Undefined to make the codec obey the codecs laws (https://github.com/gcanti/io-ts/blob/master/Codec.md#codec-interface).

Therefore this is not a real codec. Maybe something better can be done implementing the invariant mapping (#7)

@ilario-pierbattista ilario-pierbattista merged commit 4a12b5a into master Apr 6, 2021
@ilario-pierbattista ilario-pierbattista deleted the 2104_#6_undefinable branch April 6, 2021 07:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[combinator] something like partial in io-ts.
1 participant