Stuart B
Stuart B
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:
const form_schema = type({
name: 'string.alpha>2',
email: ['string.email','@','Please enter your work email address']
'age': 'number>17'
});
const form_schema = type({
name: 'string.alpha>2',
email: ['string.email','@','Please enter your work email address']
'age': 'number>17'
});
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.
const personSchema = type({
name:'string',
age:'0<=number<=120'
})
const personSchema = type({
name:'string',
age:'0<=number<=120'
})
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:
const formDataParser=type('parse.formData')
const coerce=type({'age?':'parse.integer'})
const parsedFormInput = formDataParser(formInput).pipe(coerce).pipe(personSchema)
const formDataParser=type('parse.formData')
const coerce=type({'age?':'parse.integer'})
const parsedFormInput = formDataParser(formInput).pipe(coerce).pipe(personSchema)
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