Handling branded types with optional and nullish values can indeed be a bit tricky, especially wh...
I have a question regarding branded types and Schema.optionalToOptional. I get data from extern and it can be in a wild shape sometimes.
- Strings can be null
- Strings can be missing
- Strings can be empty
The first two requirements are simple, they can be represented using
Now for the last part. Removing empty strings is recommended in the Docs by using
So if I want to treat null as missing, empty string as missing (and missing strings as missing), I came to this solution:
Naming might not be accurate, I am still working on this. But the gist is: Any null value is omitted, and then null or empty strings are also omitted. Only if there actually is a non empty string, we keep it. This is awesome!
But with this I loose the ability to brand my strings (or at least I do not know how, this is the part that I struggle with).
Do I really need to define this Schema for every of my brand schemas?
Any help is appreciated!
- Strings can be null
- Strings can be missing
- Strings can be empty
The first two requirements are simple, they can be represented using
Schema.NullishOr(Schema.String). So whether I expect a branded type or not I can do either Schema.NullishOr(Schema.String) or Schema.NullishOr(Schema.String.pipe(Schema.fromBrand(myBrand)). So far so good.Now for the last part. Removing empty strings is recommended in the Docs by using
Schema.requiredToOptional or Schema.optionalToOptional.So if I want to treat null as missing, empty string as missing (and missing strings as missing), I came to this solution:
Naming might not be accurate, I am still working on this. But the gist is: Any null value is omitted, and then null or empty strings are also omitted. Only if there actually is a non empty string, we keep it. This is awesome!
But with this I loose the ability to brand my strings (or at least I do not know how, this is the part that I struggle with).
Do I really need to define this Schema for every of my brand schemas?
Any help is appreciated!
