Skip to content

Commit a805f0b

Browse files
NicolappsConvex, Inc.
authored andcommitted
Fix defineTable({ ...v.object({}) }) handling (#37561)
When writing code like `defineTable({ ...v.object({}) })`, the validator’s attributes are copied but not getters, including `json` which is used during the table definition export. This means that `export` would silently fail, as it did for someone on Discord (https://discord.com/channels/1019350475847499849/1019350478817079338/1374853725491036270). Other schema definition errors are handled by the backend, but this one isn’t since `document_type: None` is a valid attribute value in `TableDefinitionJson`. `export` in `TableDefinition` is the only `export` method where we’re calling a JavaScript getter, so other code paths should be fine. GitOrigin-RevId: 392fa3b7cbd1939dae8349e01dcde8ea2e29c079
1 parent 5cdc883 commit a805f0b

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

npm-packages/convex/src/server/schema.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,3 +749,13 @@ describe("defineSchema/defineTable expose table validators", () => {
749749
expect(schema.tables.table.validator).not.toHaveProperty("_creationTime");
750750
});
751751
});
752+
753+
test("defineTable fails if it can’t export the validator", () => {
754+
const table = defineTable(
755+
// @ts-expect-error
756+
{ ...v.object({}) }, // This will clone `isConvexValidator` but not the `json` getter used by `export`
757+
);
758+
expect(() => table.export()).toThrow(
759+
"Invalid validator: please make sure that the parameter of `defineTable` is valid (see https://docs.convex.dev/database/schemas)",
760+
);
761+
});

npm-packages/convex/src/server/schema.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,18 @@ export class TableDefinition<
310310
* @internal
311311
*/
312312
export() {
313+
const documentType = this.validator.json;
314+
if (typeof documentType !== "object") {
315+
throw new Error(
316+
"Invalid validator: please make sure that the parameter of `defineTable` is valid (see https://docs.convex.dev/database/schemas)",
317+
);
318+
}
319+
313320
return {
314321
indexes: this.indexes,
315322
searchIndexes: this.searchIndexes,
316323
vectorIndexes: this.vectorIndexes,
317-
documentType: this.validator.json,
324+
documentType,
318325
};
319326
}
320327
}

0 commit comments

Comments
 (0)