Stuart B
Aarktype
•Created by Stuart B on 9/30/2024 in #questions
Identifying a Date value in an object type
If I iterate through an object type, I can look at each key:value and do this:
How am I meant to check if a value is a Date?
I have something like this:
If I iterate through the keys of a type like this looking for a Date type, I can't match it in the way I can match Boolean or number.
Neither of these work.
39 replies
Aarktype
•Created by Stuart B on 9/25/2024 in #questions
Post-morph constraints not checked if constraints not met on another property
If I have:
If I then try:
I will get an error for name, but not for age.
If I instead try
Then I now get an error about age being <18, now that the 'name' check has passed.
Is this by design?
188 replies
Aarktype
•Created by Stuart B on 9/5/2024 in #questions
Any way to tell if a custom description has been provided?
Is there any way to tell if a custom description has been created for a type?
For example, let's say I have this, which I'm using to validate some user input:
If the name or age are incorrect I want to display the normal composite messages (the .message property)
If email is incorrect, I want to display only the description I've entered (the .description property), not including the "must be" and "was" parts.
The problem is that .description is populated even if I havent provided a custom description, so I can't make the choice of which to display dynamically.
Is there any property/method in ArkErrors that will tell me?
11 replies
Aarktype
•Created by Stuart B on 8/23/2024 in #questions
Automatically applying parse.integer
Let's say I have a type I'm using to validate some API, e.g.
At some point, I also want to use this type to validate form input. As it's formData,the age will be a string not a number.
I know I can do something like this:
However, is there a way to somehow automate the coerce step? I have lots of these types (some of them dynamically created) and I'd rather avoid having to manually create an extra type to pick out the numeric fields and parse.integer them.
Maybe there's some way I can automatically pick out the number properties from a type and automatically build the coerce step so that parse.integer is only applied to any properties that are numeric in the final schema?
I'm fairly new to this, so it could just be my lack of Typescript skill. Any pointers would be much appreciated.
160 replies
Aarktype
•Created by Stuart B on 8/8/2024 in #questions
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.
12 replies