{"record":{"id":"56caabb87369c549","repo":"siyuan-note/siyuan","slug":"invalid-view-state-field","errorCode":null,"errorMessage":"Invalid view state field","messagePattern":"Invalid view state field","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"app/src/util/viewState.ts","lineNumber":70,"sourceCode":"        checkResponse(response, \"get\");\n        return isViewStateData(response.data) ? response.data : {};\n    },\n    async patch(key, values, removeKeys) {\n        const {fetchSyncPost} = await import(\"./fetch\");\n        const response = await fetchSyncPost(\"/api/storage/patchViewState\", {key, values, removeKeys});\n        checkResponse(response, \"patch\");\n    },\n};\n\nconst validateIdentityPart = (name: string, value: string) => {\n    if (!value) {\n        throw new Error(`View state ${name} must not be empty`);\n    }\n};\n\nconst validateField = (field: string) => {\n    if (!field || field.length > 2048) {\n        throw new Error(\"Invalid view state field\");\n    }\n};\n\nexport const getViewStateKey = (identity: IViewStateIdentity) => {\n    validateIdentityPart(\"scope\", identity.scope);\n    validateIdentityPart(\"surface\", identity.surface);\n    validateIdentityPart(\"hostID\", identity.hostID);\n    const key = [identity.scope, identity.surface, identity.hostID].map(encodeURIComponent).join(\":\");\n    if (key.length > 1024) {\n        throw new Error(\"View state key is too long\");\n    }\n    return key;\n};\n\nexport class ViewStateService {\n    public readonly key: string;\n    public readonly ready: Promise<void>;\n    private readonly data: TViewStateData = {};","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/app/src/util/viewState.ts#L52-L88","documentation":"validateField constrains each view state field name to a non-empty string of at most 2048 characters. Fields are persisted as storage record keys, so empty or oversized names would break storage semantics. The error is thrown synchronously by has/get/set/patch before any network request.","triggerScenarios":"Calling service.has/get/set/patch (via validateField, app/src/util/viewState.ts:70) with field === \"\" or a field name longer than 2048 chars — e.g. building keys by concatenation where a segment was undefined (\"undefined\" is fine, but template gaps yield empty strings), or accidentally passing a serialized value instead of a name.","commonSituations":"Dynamic field names from user data or long IDs; joining path segments where one is empty; passing an object or JSON blob as the field argument by mistake.","solutions":["Check field is a non-empty string of length <= 2048 before calling the API","Use short, stable field names; store large values as the value, not the key","Sanitize dynamic key parts (trim, cap length) at the component boundary","Catch the error and fall back to a default field name"],"exampleFix":"// before\nservice.set(`${longPrefix}:${id}`, value); // > 2048 chars\n// after\nservice.set(id.slice(0, 64), value);","handlingStrategy":"validation","validationCode":"const safeField = (f) => (typeof f === \"string\" && f.length > 0 && f.length <= 2048) ? f : \"default\";\nservice.set(safeField(myField), value);","typeGuard":"const isValidField = (f) => typeof f === \"string\" && f.length > 0 && f.length <= 2048;","tryCatchPattern":"try { service.get(field); } catch (e) { if (e.message === \"Invalid view state field\") service.get(\"default\"); }","preventionTips":["Use short static field names; keep dynamic data in values","Sanitize/trim dynamic key segments before use","Never pass serialized values as field names"],"tags":["validation","view-state","key-length"],"backgroundTag":"invalid-argument-format","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}