siyuan-note/siyuan · error
invalid view state data key
Error message
invalid view state data key
What it means
PatchViewState validates every key in values and every entry in removeKeys with validateViewStateDataKey, which rejects keys that are empty/whitespace or longer than 2048 characters. The whole patch is rejected if any single data key is malformed.
Solutions
- Sanitize or hash over-long dynamic keys to fit within 2048 characters before patching
- Filter out empty/blank keys from values and removeKeys before calling
- Ensure removeKeys omits (not includes) empty entries when no removal is intended
Example fix
// before
removeKeys := []string{"", "theme"}
PatchViewState(key, values, removeKeys)
// after
removeKeys := []string{"theme"} // drop empty entries
PatchViewState(key, values, removeKeys) Defensive patterns
Strategy: validation
Validate before calling
func validDataKey(k string) bool {
k = strings.TrimSpace(k)
return k != "" && len(k) <= 2048
}
// filter before patch
for k := range values { if !validDataKey(k) { delete(values, k) } }
removeKeys = slices.DeleteFunc(removeKeys, func(k string) bool { return !validDataKey(k) }) Try / catch
if _, err := PatchViewState(key, values, removeKeys); err != nil && err.Error() == "invalid view state data key" {
// sanitize keys and retry
} Prevention
- Sanitize dynamic keys built from user content before patching
- Never append empty placeholder strings to removeKeys
- Hash over-long keys instead of using raw URLs/text as keys
When it happens
Trigger: Patching view state where at least one values key or removeKeys entry is ""/blank or exceeds 2048 chars — e.g. dynamic keys built from user content, serialized objects as keys, or a nil/absent removeKeys element.
Common situations: Using UI text or URLs directly as state keys; accidental empty-string keys from destructuring/loop bugs; appending placeholder "" entries to removeKeys when nothing should be removed.
Understand the failure class
Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.
Related errors
- invalid view state key
- Invalid view state field
- View state key is too long
- View state must not be empty
- 106
AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19).
Data as JSON: /api/errors/d6a34e9686f2f49f.
Report an issue: GitHub.
Appendix: source
Thrown at kernel/model/view_state.go:220
return int64(len(keys) + 1)
}
if ret <= state.Updated {
ret = state.Updated + 1
}
}
return ret
}
func validateViewStateKey(key string) error {
if "" == strings.TrimSpace(key) || 1024 < len(key) {
return errors.New("invalid view state key")
}
return nil
}
func validateViewStateDataKey(key string) error {
if "" == strings.TrimSpace(key) || 2048 < len(key) {
return errors.New("invalid view state data key")
}
return nil
}
func pruneViewStates(views map[string]*ViewState) {
if len(views) <= maxViewStateCount {
return
}
keys := make([]string, 0, len(views))
for key := range views {
keys = append(keys, key)
}
sort.Slice(keys, func(i, j int) bool {
left, right := views[keys[i]], views[keys[j]]
if left.Updated == right.Updated {
return keys[i] < keys[j]
}View on GitHub (pinned to 9f775e8a12)