{"record":{"id":"fa6238da426ac78d","repo":"siyuan-note/siyuan","slug":"invalid-view-state-key","errorCode":null,"errorMessage":"invalid view state key","messagePattern":"invalid view state key","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/view_state.go","lineNumber":213,"sourceCode":"\t\t\t\t\treturn keys[i] < keys[j]\n\t\t\t\t}\n\t\t\t\treturn left.Updated < right.Updated\n\t\t\t})\n\t\t\tfor i, key := range keys {\n\t\t\t\tviews[key].Updated = int64(i + 1)\n\t\t\t}\n\t\t\treturn int64(len(keys) + 1)\n\t\t}\n\t\tif ret <= state.Updated {\n\t\t\tret = state.Updated + 1\n\t\t}\n\t}\n\treturn ret\n}\n\nfunc validateViewStateKey(key string) error {\n\tif \"\" == strings.TrimSpace(key) || 1024 < len(key) {\n\t\treturn errors.New(\"invalid view state key\")\n\t}\n\treturn nil\n}\n\nfunc validateViewStateDataKey(key string) error {\n\tif \"\" == strings.TrimSpace(key) || 2048 < len(key) {\n\t\treturn errors.New(\"invalid view state data key\")\n\t}\n\treturn nil\n}\n\nfunc pruneViewStates(views map[string]*ViewState) {\n\tif len(views) <= maxViewStateCount {\n\t\treturn\n\t}\n\n\tkeys := make([]string, 0, len(views))\n\tfor key := range views {","sourceCodeStart":195,"sourceCodeEnd":231,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/view_state.go#L195-L231","documentation":"validateViewStateKey rejects a view state key that is empty/whitespace or longer than 1024 characters. GetViewState, PatchViewState, and RemoveViewState all call it first, so any view-state operation with a malformed key fails with this error.","triggerScenarios":"Calling GetViewState/PatchViewState/RemoveViewState with \"\" or a blank string, or with a key exceeding 1024 chars (e.g. an entire serialized object used as the key, or concatenated IDs).","commonSituations":"Frontend code building keys by joining IDs without bounds; passing an uninitialized variable as the key; forgetting the key argument in an API request so the empty string is validated.","solutions":["Ensure the key is a non-empty, trimmed identifier before calling (e.g. a document/view ID)","Truncate or hash over-long composite keys to keep them under 1024 characters","Validate the key client-side and skip the API call when it is blank"],"exampleFix":"// before\nGetViewState(strings.Join(longParts, \"/\")) // > 1024 chars\n// after\nk := strings.Join(longParts, \"/\")\nif len(k) > 1024 { k = k[:512] + \"__\" + hash(k) }\nGetViewState(k)","handlingStrategy":"validation","validationCode":"func validViewKey(k string) bool {\n    k = strings.TrimSpace(k)\n    return k != \"\" && len(k) <= 1024\n}\nif !validViewKey(key) { return errors.New(\"invalid view state key\") }","typeGuard":null,"tryCatchPattern":"if _, err := GetViewState(key); err != nil && err.Error() == \"invalid view state key\" {\n    // fix or regenerate the key before retrying\n}","preventionTips":["Validate keys at construction time in frontend code","Hash or truncate composite keys to stay under 1024 chars","Never pass empty/uninitialized variables as view keys"],"tags":["view-state","validation","key"],"backgroundTag":"invalid-identifier-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"}