Gorka CesiumG
Convex Community3y ago
6 replies
Gorka Cesium

union kind

https://docs.convex.dev/database/schemas#unions

example from the docs
defineTable(
  v.union(
    v.object({
      kind: v.literal("StringDocument"),
      value: v.string(),
    }),
    v.object({
      kind: v.literal("NumberDocument"),
      value: v.number(),
    })
  )
);


in this example I wonder if
value
can be a
v.object
instead of
v.string
and
v.number
.

I also wonder if the
kind
field has to be always named
kind
or can it be also
tag
?

I'm exploring how to map this to Rescript Variants

I would like to do something like this


let jobWindow = {
  width: v.number(),
  height: v.number()
}
let jobProduct = {
  qty: v.number(),
  name: v.string()
}
defineTable({
 v.union(
    v.object({
      tag: v.literal("JobWindow"),
      value: jobWindow,
    }),
    v.object({
      tag: v.literal("JobProduct"),
      value: jobProduct,
    })
  )
});
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?