ChrisLi
ChrisLi4w ago

Is that possible to convert a type to validator?

Hi, to better maintain types in project, I wonder if there is a way to convert types to validators?
5 Replies
Convex Bot
Convex Bot4w ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
Clever Tagline
Would it work for your use case to do the reverse, turning validators into types? Here's an example how to do that:
export const taskGroup = v.union(
v.literal("Personal"),
v.literal("Work"),
v.literal("Family"),
v.literal("Church")
)

export type TaskGroup = Infer<typeof taskGroup>
export const taskGroup = v.union(
v.literal("Personal"),
v.literal("Work"),
v.literal("Family"),
v.literal("Church")
)

export type TaskGroup = Infer<typeof taskGroup>
There may be a way to go the direction you asked. I just haven't played with it yet (or searched for a solution) to find out how.
ballingt
ballingt4w ago
There's not unfortunately, types in TypeScript don't exist at runtime. There are a variety of ways to try to do this with codegen/bundler extensions but nothing supported now.
ChrisLi
ChrisLiOP4w ago
thanks for your answers, I will take a look at other options!
ChrisLi
ChrisLiOP4w ago
actually, I looking for a solution to manage tyes and validators, and I found the following article, and it helps me a lot: https://stack.convex.dev/types-cookbook
Types and Validators in TypeScript: A Convex Cookbook
It can be tough to wrangle types to behave how you want them to. Thankfully, Convex was designed to make the experience with types perfect. Learn why ...

Did you find this page helpful?