{"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":"Error","httpStatus":null,"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/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/models/memory.js#L17-L53","documentation":"Thrown by the Memory model's scope validator. VALID_SCOPES is fixed to ['workspace', 'global']; any other value (including undefined-coerced strings, typos, or a stale enum) is rejected with the offending value JSON-stringified for diagnostics. The validator is the default for the scope field across create/update paths.","triggerScenarios":"Calling Memory.create / update with scope set to something other than 'workspace' or 'global' — e.g. 'Workspace' (wrong case), 'user', null coerced to 'null', or an integer. Also when client code reads scope from an unvalidated request body.","commonSituations":"Frontend dropdown sent a display label instead of the enum value; a refactor introduced a new scope tier without updating VALID_SCOPES; deserialization produced undefined and it was passed through.","solutions":["Pass exactly 'workspace' or 'global' (lowercase).","Validate scope against the allowed list on the API boundary before reaching the model.","If a new scope is genuinely needed, add it to Memory.VALID_SCOPES and update the DB enum/check constraint.","Default the parameter when optional: scope ?? 'workspace'."],"exampleFix":"// before\nawait Memory.create({ userId, scope: req.body.scope, content });\n// after\nconst scope = ['workspace','global'].includes(req.body.scope) ? req.body.scope : 'workspace';\nawait Memory.create({ userId, scope, content });","handlingStrategy":"validation","validationCode":"const VALID_SCOPES = ['workspace', 'global'];\nconst scope = VALID_SCOPES.includes(input.scope) ? input.scope : 'workspace';\n// pass `scope` to Memory.create","typeGuard":"function isValidMemoryScope(v) {\n  return v === 'workspace' || v === 'global';\n}","tryCatchPattern":null,"preventionTips":["Validate scope at the API boundary against the allowed enum.","Keep VALID_SCOPES as the single source of truth; import it where needed.","Default optional scope parameters to 'workspace'."],"tags":["memory","validation","enum","input-validation"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}