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:
{
param1: "",
param2: "2,3,5",
}
{
param1: "",
param2: "2,3,5",
}
If param2 fails and fallback to undefined, I want the whole object to still parse and return { param1: "", param2: undefined }
12 Replies
TizzySaurus
TizzySaurus6d ago
You can implement custom logic like this with a pipe (morph): https://arktype.io/docs/intro/morphs-and-more
ArkType
ArkType Docs
TypeScript's 1:1 validator, optimized from editor to runtime
TizzySaurus
TizzySaurus6d ago
const stringNumberArray = type("string.numeric.parse[]")

const foo = type({
param1: "string",
param2: ["string", "=>", (data) => {
const split = data.split(","));

const asNumbers = stringNumberArray(split);
return (asNumbers instanceof type.errors) ? undefined : asNumbers;
}]
})
const stringNumberArray = type("string.numeric.parse[]")

const foo = type({
param1: "string",
param2: ["string", "=>", (data) => {
const split = data.split(","));

const asNumbers = stringNumberArray(split);
return (asNumbers instanceof type.errors) ? undefined : asNumbers;
}]
})
or w/e
ssalbdivad
ssalbdivad6d ago
Yeah this is almost exactly what I wrote:
import { type } from "arktype"

const parseToIntegers = type("string.integer.parse[]")

const T = type({
param1: "string",
param2: type.string.pipe(s => {
const substrings = s.split(",")
const parsed = parseToIntegers(substrings)
if (parsed instanceof type.errors) return undefined
return parsed
})
})
import { type } from "arktype"

const parseToIntegers = type("string.integer.parse[]")

const T = type({
param1: "string",
param2: type.string.pipe(s => {
const substrings = s.split(",")
const parsed = parseToIntegers(substrings)
if (parsed instanceof type.errors) return undefined
return parsed
})
})
Only meaningful difference is string.integer.parse if you need them to be integers
rostero
rosteroOP6d ago
very nice. thank you both Haven't tried ark before this. I'm sure I could figure this out, but
const out = Thing({
param1: "TypeScript",
param2: 3
})
const out = Thing({
param1: "TypeScript",
param2: 3
})
Causes the whole thing to fail instead of returning { param1: "TypeScript", param2: undefined }. I forgot to mention that requirement
ssalbdivad
ssalbdivad6d ago
That 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
rostero
rosteroOP6d ago
hmm.. I may be copying and pasting wrong ahh.. hotlinking doesn't seem to be working
ssalbdivad
ssalbdivad6d ago
Are you clicking this?
No description
ssalbdivad
ssalbdivad6d ago
Oh I see you're saying that shouldn't fail Then you should just pipe from unknown instead and return undefined in that case
rostero
rosteroOP6d ago
yeah, 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

Did you find this page helpful?