#what is the convex util to check if an Id is of a specific table?

6 messages · Page 1 of 1 (latest)

livid oarBOT
#

Hi @storm shadow. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Convex provides ctx.db.normalizeId for this. It lets you check whether a string ID belongs to a specific table:

const normalized = ctx.db.normalizeId("users", someIdString);
if (normalized === null) {
  // not a valid users ID (wrong table or invalid format)
} else {
  // normalized is a GenericId<"users"> you can safely use with db.get/patch/etc.
}

normalizeId returns null if the ID is from a different table or not a valid Convex ID, otherwise it returns a typed GenericId<"tableName">. [normalizeId reader; normalizeId writer]

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@storm shadow Kapa.ai is still learning and improving, please let me know how I did by reacting below.

storm shadow
#

is it working in the client as well? @livid oar

livid oarBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

The knowledge sources only document db.normalizeId on the server-side database interfaces (GenericDatabaseReader / GenericDatabaseWriter), i.e. inside Convex functions using ctx.db. [normalizeId reader; normalizeId writer]

They do not mention an equivalent utility on the JavaScript/React client, so I can’t confirm that normalizeId (or a similar helper) is available in client code.

On the client, the documented pattern is:

  • Treat IDs as strings at runtime.
  • Use TypeScript’s Id<"table"> type for static typing.
  • When you receive an ID string from outside Convex, pass it into a Convex function and validate it there using either:
    • an argument validator like v.id("tasks"), or
    • ctx.db.normalizeId("tasks", idString) inside the handler. [Document IDs]

So: normalizeId is documented for server-side use; there’s no information in these sources that it works directly in client code.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: