{"record":{"id":"80b8057de4052960","repo":"Mintplex-Labs/anything-llm","slug":"invalid-scope-json-stringify-v","errorCode":null,"errorMessage":"Invalid scope: ${JSON.stringify(v)}","messagePattern":"Invalid scope: (.+?)","errorType":"validation","errorClass":null,"httpStatus":400,"severity":"warning","filePath":"server/models/memory.js","lineNumber":35,"sourceCode":"  if (!Number.isInteger(n))\n    throw new Error(`Expected integer, got ${JSON.stringify(v)}`);\n  return n;\n}\n\nconst Memory = {\n  GLOBAL_LIMIT: 5,\n  WORKSPACE_LIMIT: 20,\n  MAX_INJECTED_WORKSPACE_LIMIT: 5,\n  VALID_SCOPES: [\"workspace\", \"global\"],\n\n  validations: {\n    id: (v) => toInt(v),\n    userId: (v = null) => (v === null || v === undefined ? null : toInt(v)),\n    workspaceId: (v = null) =>\n      v === null || v === undefined ? null : toInt(v),\n    scope: (v = \"workspace\") => {\n      if (!Memory.VALID_SCOPES.includes(v))\n        throw new Error(`Invalid scope: ${JSON.stringify(v)}`);\n      return v;\n    },\n    content: (v) => {\n      if (typeof v !== \"string\" || v.trim().length === 0)\n        throw new Error(\"Content must be a non-empty string\");\n      return v;\n    },\n  },\n\n  /**\n   * List a user's workspace-scoped memories, newest first.\n   * @param {number|null} userId\n   * @param {number} workspaceId\n   * @returns {Promise<Memory[]>}\n   */\n  forUserWorkspace: async function (userId, workspaceId) {\n    try {\n      const memories = await prisma.memories.findMany({","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/f92433b4ea0598492a1e6645ea22addbb4dd1287/server/models/memory.js#L17-L53","documentation":"Memory's scope validator rejects any value not in VALID_SCOPES (['workspace', 'global']). It runs when creating or updating memory entries; the parameter defaults to 'workspace' only when omitted entirely, so an explicit wrong value - including null - reaches this throw. The message embeds JSON.stringify(v) so you can see exactly what was received.","triggerScenarios":"Passing 'Workspace' or 'GLOBAL' (case-sensitive check); using 'document' or 'user' as a scope; explicitly passing null (JSON.stringify shows \"null\"); values from an unvalidated enum in the client.","commonSituations":"Client enums drifting from the server's allowed list; casing differences after a refactor; passing 0, false, or other falsy garbage from form defaults.","solutions":["Use exactly 'workspace' or 'global', lowercase","Omit the scope argument when you want the 'workspace' default","Normalize/validate scope on the client against the same allowed list"],"exampleFix":"// before\nMemory.create({ userId, workspaceId, scope: 'Workspace', content: '...' }); // throws: Invalid scope: \"Workspace\"\n\n// after\nMemory.create({ userId, workspaceId, scope: 'workspace', content: '...' });","handlingStrategy":"type-guard","validationCode":"const VALID_SCOPES = ['workspace', 'global'];\n\nif (scope !== undefined && !VALID_SCOPES.includes(scope)) {\n  return res.status(400).json({ error: `scope must be one of ${VALID_SCOPES.join(', ')}` });\n}","typeGuard":"/** @param {unknown} v */\nfunction isMemoryScope(v) {\n  return v === 'workspace' || v === 'global';\n}\n\n// usage: if (scope !== undefined && !isMemoryScope(scope)) throw new RangeError('bad scope');","tryCatchPattern":"try {\n  await Memory.create({ userId, workspaceId, scope, content });\n} catch (err) {\n  if (/Invalid scope:/.test(err.message)) {\n    return res.status(400).json({ error: 'scope must be workspace or global' });\n  }\n  throw err;\n}","preventionTips":["Define the allowed scope list once and share it between client validation and server calls","Omit scope entirely when the 'workspace' default is wanted - do not send null","Keep casing lowercase everywhere; treat enum drift between client and server as a build error"],"tags":["memory","validation","enum"],"backgroundTag":"invalid-enum-value","analyzedSha":"f92433b4ea0598492a1e6645ea22addbb4dd1287","analyzedAt":"2026-08-18T10:02:21.017Z","contentChangedAt":"2026-08-18T10:02:21.017Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}