v.array schema validator semantics
@zid asked:
Question about defining arrays in schemas.
Will the following allow for an empty array of items/objects?
If not, how do I write the schema so that I can a default value of an empty array?
Also would like to confirm if the below allows for a dynamic number of array items.
arrayItems: v.array(v.object({id: v.string(), title: v.string()}) )
2 Replies
Will the following allow for an empty array of items/objects?Yes! You can write a document with an empty array into the DB. If you have some existing data in the table and you're trying to add this field, you can wrap the v.array in
v.optional(....)
so that the existing data is still valid.
More info here:
https://stack.convex.dev/migrating-data-with-mutations
Also would like to confirm if the below allows for a dynamic number of array items.Yes as well. Feel free to play with the validators in your dev deployment, or spin up a new project to verify the semantics.
Migrating Data With Mutations
Using mutations to migrate data in Convex.
@Michal Srb Ah, thank you Michal!