Sara
Sara2mo ago

Validate Id<"tableName"> or Doc<"tableName"> on the client or server?

How can we validate the type of Id or Doc in the client with typescript? I've tried instanceOf, and casting the type but it gives me an error that the type passed is of type any, The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter.ts(2358)
if(args.Id instanceof Id<"tableName">)
if(args.Id instanceof Id<"tableName">)
9 Replies
Convex Bot
Convex Bot2mo 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!
ian
ian2mo ago
On the server you can use ctx.db.normalizeId to tell if the ID is for the right table. On the client you can't tell what table it is for, but you can check that the id is a string, like doc._id instanceof string. Validating the shape of a document is a little different - they're all just Objects with different fields. In which situations are you trying to differentiate what kind of id/ document it is? I'm not sure what args.Id is here, but it's important to note that Id<"tableName"> doesn't exist at runtime. It's just a type for typechecking. Once the code is bundled for the client, all the typescript is gone and there is no runtime check that looks at typescript types. So you'd need to have something available at runtime, like a class, or primitive like string or Object
Sara
SaraOP2mo ago
I see! I was trying to handle a case where the type of the param can be changing, and if it changed to a misleading Id like: jh762hfgeptvssgrjew instead of jh762hfgeptvssgrjews5j70nh75yex0 it should atleast throw a not found error, but In my case it will throw a validator error and I'm already checking the type to be undefined or not in the client side
ian
ian2mo ago
gotcha. doing a string length check client-side might be the most pragmatic approach
Sara
SaraOP2mo ago
ah, is there a limit to the id length,, its 32
ian
ian2mo ago
If you see anything otherwise, report back
Sara
SaraOP2mo ago
sure thing, thanks loads!
sshader
sshader2mo ago
it should atleast throw a not found error, but In my case it will throw a validator error
Sounds like you've got this handled already, but if you want to handle "not found" on the frontend, one option is to use v.string() in your validator, and then check it with db.normalizeId + db.get, and throw a specific ConvexError that you can catch on the frontend Here's an example from one of my apps (except I didn't do the ConvexError part yet) https://github.com/sshader/js-bee/blob/76b2876cad12c8b3048077a29e9581c0cf4a9049/convex/games.ts#L173
GitHub
js-bee/convex/games.ts at 76b2876cad12c8b3048077a29e9581c0cf4a9049 ...
Contribute to sshader/js-bee development by creating an account on GitHub.
Sara
SaraOP2mo ago
This really helped quite a lot, I wish I had asked this question Earlier, thank you so much!

Did you find this page helpful?