RJR
Convex Community16mo ago
11 replies
RJ

Schema validation succeeds when adding a non-optional field to a union variant

Let's say I have a table like this:

defineSchema({
  my_table: defineTable(v.union(
    v.object({
      tag: v.literal("A"),
    }),
    v.object({
      tag: v.literal("B"),
    })
  ))
})


Which contains some documents, like these:

[{ tag: "A" }, { tag: "B" }]


If I add a new (non-optional) field to one of the variants:

defineSchema({
  my_table: defineTable(v.union(
    v.object({
      tag: v.literal("A"),
+     a_field: v.string()
    }),
    v.object({
      tag: v.literal("B"),
    })
  ))
})


Schema validation succeeds, thus permitting invalid documents in that table. Validation seems to work correctly in other contexts (e.g. insertion, patching).
Was this page helpful?