How to ensure comma delimited set of numbers or fallback to undefined?
I'm trying to parse in some query string parameters, like "2,3,5" that I want parsed to [2, 3, 5]. I want to fail if any any number doesn't parse to an integer. When it fails I want to return undefined.
Note that this will be part of a larger type:
If param2 fails and fallback to undefined, I want the whole object to still parse and return
{ param1: "", param2: undefined }
12 Replies
You can implement custom logic like this with a pipe (morph): https://arktype.io/docs/intro/morphs-and-more
or w/e
Yeah this is almost exactly what I wrote:
Only meaningful difference is
string.integer.parse
if you need them to be integersvery nice. thank you both
Haven't tried ark before this. I'm sure I could figure this out, but
Causes the whole thing to fail instead of returning
{ param1: "TypeScript", param2: undefined }
. I forgot to mention that requirementThat should be true of the current implementation
If you read about morphs and look at the way that is written, it would have to be a string otherwise it would be an error
hmm.. I may be copying and pasting wrong
ahh.. hotlinking doesn't seem to be working
Are you clicking this?

Oh I see you're saying that shouldn't fail
Then you should just pipe from
unknown
instead and return undefined in that caseyeah, looking for a fallback to default. could be undefined or another string
ok. i'll have to check out the docs later. too late in the day for me to figure this out. thanks again