mojotech / mojotech/json-type-validation
Feature: 'nullToUndefined' decoder type
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 154
- Forks
- 14
- PR merge metrics
- No merged PRs in 30d
Description
Many APIs provide missing data fields with a value of null (see issue #29).
As a consumer of such an API using TypeScript and this very library it is currently not possible to decode such values to undefined.
Personally, I prefer to use undefined instead of null for my TypeScript models and I try to avoid null wherever possible. While this might be just my own preference, I believe it would make sense to add a standard decoder to this library that will automatically transform a given null value to undefined.
Here is snippet that does exactly what I need:
const nullToUndefined = <T>(decoder: Decoder<T>): Decoder<T | undefined> =>
JTV.union(decoder, JTV.constant(null)).map((val) => val === null ? undefined : val);
I am not sure if this is the best solution to the given problem, but it works.
I am happy to file a PR if that is desired 🙂
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing issue #29 and the library's existing Decoder API, then compare the proposed nullToUndefined behavior with its current decoder patterns. Done means providing a standard decoder that converts null input to undefined while preserving other decoded values, with coverage for both cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- api
- Issue type
- Feature
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100