DelveroffD
Convex Community2y ago
7 replies
Delveroff

Schema Intersection

I try to create the following model:
type Asset = {
  status: 'created' | 'removed'
} & (
  | {
      type: 'image'
      src: string
    }
  | {
      type: 'text'
      value: string
    }
)

I've read about unions at the top level here: https://docs.convex.dev/database/schemas#unions

However, as you can see, I need the status field that is true for all asset types. I tried to do this:
defineSchema({
  assets: defineTable({
    status: v.string(), // union of literals
    ...v.union( // Argument of type is not assignable to parameter of type Record<string, GenericValidator>
      v.object({type: v.literal('image'), src: v.string()}),
      v.object({type: v.literal('text'), value: v.string()})
    )
  })
})


For now, I can't understand how to implement the aforementioned type. Seems like it lacks v.intersection() API.
Schema validation keeps your Convex data neat and tidy. It also gives you end-to-end TypeScript type safety!
Schemas | Convex Developer Hub
Was this page helpful?