Stuart B
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:
if (value.extends(type.number) { // Do some stuff
}
if (value.extends('boolean') { // Do some different stuff
}
if (value.extends(type.number) { // Do some stuff
}
if (value.extends('boolean') { // Do some different stuff
}
How am I meant to check if a value is a Date? I have something like this:
const schema=type(
{
id: 'number',
last_updated: 'Date'
}
)
const schema=type(
{
id: 'number',
last_updated: 'Date'
}
)
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.
if (value.extends('Date')) { // some stuff
}
if (value.extends(type('Date'))) { // some stuff
}
if (value.extends('Date')) { // some stuff
}
if (value.extends(type('Date'))) { // some stuff
}
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:
const schema = type({
name: 'string>3',
'age?': type('string.numeric.parse').to('number>18')
});
const schema = type({
name: 'string>3',
'age?': type('string.numeric.parse').to('number>18')
});
If I then try:
const result=schema({
name:'me',
age:'5'
})
const result=schema({
name:'me',
age:'5'
})
I will get an error for name, but not for age. If I instead try
const result=schema({
name:'Stuart',
age:'5'
})
const result=schema({
name:'Stuart',
age:'5'
})
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:
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