A
arktype•2mo ago
Bobakanoosh

Easier way to chain off of string.integer.parse?

I'm essentially trying to replicate this behavior from Zod:
const numberStringGreaterThan5 = z.coerce.number().gt(5)
numberStringGreaterThan5.parse("6")
const numberStringGreaterThan5 = z.coerce.number().gt(5)
numberStringGreaterThan5.parse("6")
The best way I've found to do it with Arktype is with a .narrow , but I'm wondering if theres something simpler
const numberStringGreaterThan5 = type("string.integer.parse").narrow((num, ctx) => (num > 5 ? true : ctx.mustBe("greater than 5")))
numberStringGreaterThan5("6")
const numberStringGreaterThan5 = type("string.integer.parse").narrow((num, ctx) => (num > 5 ? true : ctx.mustBe("greater than 5")))
numberStringGreaterThan5("6")
5 Replies
ssalbdivad
ssalbdivad•2mo ago
Check out the last example on the morphs and more page. .to allows you to pipe a morph to another definition directly I will be sure to add a section for that to the primary expression docs as well
TizzySaurus
TizzySaurus•2mo ago
I guess type("string.integer.parse).to("number>5") is what you're after
Bobakanoosh
BobakanooshOP•2mo ago
I SWEAR i tried that 😭 😭 thanks!
ssalbdivad
ssalbdivad•2mo ago
I hacked into your local network and had previously modified the source to not enforce anything, just switched it back
Bobakanoosh
BobakanooshOP•2mo ago
😂 not completely convinced its not true.. I think i saw a diff example that was .to. and i tried that and saw nothing useful to chain off that

Did you find this page helpful?