Parse / validate urlencoded object
When posting something like {foo[a]: 1, foo[b]: 2}, is it possible to constrain the keys to always have a string and the value to always be a number? In the same move, is it possible to convert that to a mapped object like
qs
would too? (foo: {a: 1, b: 2}
)9 Replies
I guess you mean something like?
Ahh regex, that makes sense - Tho I dont think that would work for me since I need to validate additional known-keys as well. What I did now is use narrow - Not sure if that is ideal, if you know a better solution I'd appreciate that!
Something likeshould work
I don't remember the exact syntax for in-line regex keys but it is possible
You should use .pipe not .narrow btw
narrow is intended for validation checks, whereas pipe is to actually change the value of the output
Gotcha, switched to pipe instead thanks - I couldnt find anything like your example in the docs tho (regexp as key) fwiw
Ultimately I've ended up with this now (Added it as an optional field to the main validator so that its typed / known):
I don't think that's right.
.pipe
takes a ctx
, that has a data
property. So at a minimum you'd need ({data}) => {...}
It does work for me 🤷♂️
https://arktype.io/docs/objects#properties-index
Looks like it might be
{"[/[a-z]+/]": "string"}
for regex index signaturesAh, I'm wrong, it takes
(data, ctx)
. My bad
Otherwise foo
should technically be unknown[]
by best practice, and I would prefer Object.keys(data).forEach(key => {...})
over for (const key of Object.keys(data)) {...}
but that's personal preference
In fact, you should be able to type foo
as [string, number][]
or whatever it isI'll have a look, thank you!