ianpaschal
ianpaschal2w ago

Help understanding 'Validator error: Expected ID for table...'

If you query a document with by an ID, if the ID exists on the table, you get the document back. If it doesn't, you get null. However when testing, when I want to test that my function returns null (or throws a specific error) if the ID in the args is invalid, I instead get the error 'Validator error: Expected ID for table...' What is the purpose of this error? And why does it behave differently? Shouldn't passing 'not_a_valid_foo_id' for an arg fooId with type v.id('foos')and then calling ctx.db.get(fooId) just return null and I can handle that as I like? Why does it throw an error? Thanks in advance!
7 Replies
Convex Bot
Convex Bot2w ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
djbalin
djbalin2w ago
If I understand you correctly, I think it's because validation of the id argument passed to ctx.db.get only occurs in TypeScript, and since ids are really just strings, you can appease typescript by doing something like: const doc = await ctx.db.get("blablabla" as Id<"myTable">) So you would want to use normalizeId https://docs.convex.dev/api/interfaces/server.GenericDatabaseReader#normalizeid to check that the argument passed actually could be an id pointing to that table
djbalin
djbalin2w ago
Using v.id("foo") in the arguments to a wrapping convex query/mutation should catch this however - could you share the code?
ianpaschal
ianpaschalOP2w ago
I don't think the code helps. It's a pretty abstract question. Basically I'm trying to understand why calling ctx.db.get(id) would ever return null. Because it seems like if id is a legitimate ID, it will return the Doc and if it's not, it will throw an error.
erquhart
erquhart2w ago
The id validator does not confirm whether there is a document in the db for a given id, it only confirms that the string is a branded string id for that specific table, which I believe is a static check. And it is both typesafe and a runtime check. So if a document has an id for another document, but that other document gets deleted, you may still end up getting that id in an args validator where validation would pass, but then passing it to ctx.db.get() would result in null because the document no longer exists.
ianpaschal
ianpaschalOP2w ago
I'm afraid I don't follow... Wouldn't the subsequent .get() for the referenced ID also throw that error?
erquhart
erquhart2w ago
No because .get doesn’t accept a table name for validating the id for a specific table, it just checks for a record. It sounds like you shouldn’t use the id validator for your function arg type, just use string and then you can handle the null as you mentioned in your OP.

Did you find this page helpful?