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.
7 Replies
TizzySaurus
TizzySaurus4mo ago
Iirc there's something along the lines of type("Parse.integer").constrain({min: 3, max: 8})
Stuart B
Stuart BOP4mo ago
Maybe, but having to chain .constrain() is a problem, as I want to store the type as JSON and load it into type() at runtime. I thought I had it with {seq_num_length: '3<=(parse.integer)<=8'} which doesn't highlight any errors. But it just crashes at runtime with: "ParseError: MaxLength operand must be a string or an array (was never)"
TizzySaurus
TizzySaurus4mo ago
I don't think that's going to be possible...
const t = type(["parse.integer", "=>", n => n >= 3 && n <= 8])
// or type("parse.integer").pipe(n => n >= 3 && n <= 8)
const t = type(["parse.integer", "=>", n => n >= 3 && n <= 8])
// or type("parse.integer").pipe(n => n >= 3 && n <= 8)
this works, but you can't store functions in JSON
TizzySaurus
TizzySaurus4mo ago
If you want to store the definition in JSON, your best bet is to wait for https://github.com/arktypeio/arktype/issues/729 to be done (it's currently blocked by https://github.com/arktypeio/arktype/issues/1033)
GitHub
Support JSON-schema as an input format · Issue #729 · arktypeio/ark...
This would be a large feature allowing JSON schemas as an alternative definition format, that would be statically validated and inferred, similarly to the primary TS-based syntax. Some additional i...
Stuart B
Stuart BOP4mo ago
I worked out a suitable answer in the end. const parser = type({ name: 'parse.integer' }).pipe(type({ name: '3<=integer<=8' })); It means I need to separate any type coercion from applying constraints, but both parts can be stored in JSON easily. Thanks for the help.
ssalbdivad
ssalbdivad4mo ago
Sorry I had missed this, but I'm also wrapping up JSON schema serialization now! ArkType's builtin .json representation will be more accurate and preserve more metadata, but in case you need the other format it will be available soon (with restrictions for a lot of non-serializable values/conditions). Also a quick shortcut for adding output validation to a morph is .to, which avoids having to wrap it in an additional type call (see the last example here: https://arktype.io/intro/morphs-and-more/) So in this case:
const parser = type({ name: 'parse.integer' }).to({ name: '3<=integer<=8' })
const parser = type({ name: 'parse.integer' }).to({ name: '3<=integer<=8' })
Stuart B
Stuart BOP4mo ago
That's great. Thanks!
Want results from more Discord servers?
Add your server