oscklmO
Convex Community3y ago
8 replies
oscklm

Using v.id() in a v.union()

I'm testing out something in my dev env, trying to improve how we handle the relation between a video and a call to action. I'm considering doing the following:

Old approach using a string
export const videoValidator = v.object({
  // Other fields
  currentAction: v.optional(v.string()),
})


New approach using a union
export const callToActionValidator = v.object({
  type: v.union(
    v.literal('FunFact'),
    v.literal('Challenge'),
    v.literal('Tutorial'),
    v.literal('Quiz')
  ),
  id: v.union(
    v.id('funFacts'),
    v.id('challenges'),
    v.id('tutorials'),
    v.id('quizzes')
  ),
})


The new approach not only allows me to use the type for doing some smart dynamic rendering of a component based on the type in the frontend, and the id with the union types of the different id's for a call to action, allows me to easily delete the id in the backend.

Is there any reasons not to do it this way. Before i migrate things to this new approach.
Was this page helpful?