arktypea
arktype17mo ago
Stuart B

Convert string to number with constraints

Hi, I'm looking at switching from Zod to Arktype mainly based on the serializable nature of types (lots of database driven forms).

I'm stuck on something I think should be simple...
  • I'm passing FormData to the server.
  • Some of the fields are numeric, but are passed as strings (as FormData does)
  • I also have numeric constraints on these fields, e.g '3<= number <=8'
  • When parsing with Arktype I need to convert the strings to numeric but also check the constrains.
I can work out how to apply the required constraints to a number (as above)
I can also work out how to convert a string to a number when parsing.

But I can't work out how to do both at the same time. Can anyone help?

Ideally, I need to be able to do this just using the string syntax, rather than method chaining, as I need to be able to store the configuration of the types in a database.
Was this page helpful?