Convex schema validator
Produces a convex/schema.ts that types every document, indexes every query path, and passes validation against the data already in the database. The one rule: every withIndex in a function needs a matching .index() here, named after its fields, queried in field order.
When to reach for this
- Creating a new table or adding a field to an existing one
- A query uses
.filter()and needs an index instead - Deciding whether to embed an object or link with
v.id - Modeling a document that comes in several shapes
npx convex devfails with a schema validation error
Schema skeleton
defineTable takes either an object of field validators or a single v.union of v.object validators (see discriminated unions). Every table gets _id and _creationTime for free. Do not declare them.
Validators
Optional versus nullable
v.optional means the key may be missing from the document. v.union(t, v.null()) means the key is always present and may hold null. They are different at validation time.
Use v.optional for fields added after the table had data. Use v.union(..., v.null()) when "explicitly cleared" carries meaning.
Discriminated unions
For a table whose documents come in several shapes, pass a v.union of v.object validators to defineTable. Each member has the same literal key so TypeScript narrows on it.
Put the discriminant (kind) first in any index on a union table so queries can scope to one shape. Prefer this over one wide object full of v.optional fields.
Indexes
Naming and field order
Name the index after its fields in order: by_field1_and_field2. Querying must follow the same order: equality on a prefix of the fields, then at most one range on the next field.
You cannot skip channelId and filter on sentAt alone with that index. Add by_sentAt if that query exists. _creationTime is appended to every index automatically, so results within an equal prefix sort by creation time.
Reserved names: by_id and by_creation_time. Limits: 32 indexes per table, 16 fields per index.
When to add one
- Any field a function passes to
withIndex,.eq, or a range comparison - Every foreign key (
userId,channelId,orgId) on the child table - The sort field for a paginated list, prefixed by the scoping field
- Not for fields you only read after fetching the document
- Not for tiny tables where a
.collect()then in memory filter is fine
If a query uses .filter(), that is the signal to add an index and switch to withIndex.
Relationships
Link documents with v.id("table") on the child. Do not nest growing arrays of objects inside the parent.
Embed with v.object or a small v.array only when the data is bounded, always loaded with the parent, and updated together. A user's settings object is a good embed. A user's posts array is not: it hits the 8192 item cap and every post edit rewrites the user document.
System fields
_id: Id<"table"> and _creationTime: number (ms since epoch) exist on every document. Include them in return validators when a function returns whole documents:
Do not add your own createdAt unless you need a value that differs from insertion time.
Search and vector indexes
Declared on the table like regular indexes. filterFields must be top level fields.
Query search indexes with withSearchIndex in queries. Vector search runs only in actions via ctx.vectorSearch.
Common mistakes
Checklist
- Schema lives in
convex/schema.tsand exportsdefineSchema(...)as default - Every table has explicit field validators, no
v.any()unless justified - Every
withIndexcall inconvex/has a matching.index()with fields in the name - Foreign keys are
v.id("table")with an index on the child table - No unbounded arrays of objects embedded in a parent document
- Fields added to tables with data are
v.optional - Enums and polymorphic shapes use
v.unionofv.literalorv.objectmembers - Return validators include
_idand_creationTimewhen returning whole documents -
npx convex devpushes without a schema validation error

