{"success":true,"name":"check-localstorage-data-shape-before-reading-it-in-javascrip","description":"JS code reads localStorage and uses the value directly.","category":"ai-brimind","path":"ai-brimind/check-localstorage-data-shape-before-reading-it-in-javascrip/SKILL.md","file":"SKILL.md","content":"---\nname: check-localstorage-data-shape-before-reading-it-in-javascrip\ndescription: \"JS code reads localStorage and uses the value directly.\"\n---\n\n# Check localStorage Data Shape Before Reading It in JavaScript\n\n## When to use\nProject is JavaScript, and code reads from localStorage/sessionStorage and immediately parses or uses the result without checking it exists or has the expected shape.\n\n## Example request\nJSON.parse(localStorage.getItem('user')) → key doesn't exist yet, parse throws on null — first-visit crash. Code expects an array of objects → an older app version stored an array of IDs; .name is undefined everywhere — schema drift between versions. Two features read the same key with different assumptions → one writes, the other breaks — undocumented shared key.\n\n## Instructions\nAct like a developer who treats localStorage as untrusted input, because it is: users clear it, old app versions wrote different shapes into it, and other code may touch the same keys. Every read must survive three realities: (1) MISSING — getItem returns null on first visit or after clearing; handle it with a default before parsing; (2) CORRUPT — wrap JSON.parse in try/catch; a broken value should fall back to the default, not crash the app; (3) OUTDATED SHAPE — validate what you parsed: is it the array/object structure THIS version expects? If old versions stored a different shape, write a small migration (read old shape → convert → save new shape) instead of assuming. Centralize each key's access in one read function and one write function so the shape rules live in exactly one place, and keep a comment or constant listing all keys the app owns. Only proceed once a fresh browser profile, a corrupted value, and a legacy-shaped value all load the app without errors, and state the default used for each case.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1019\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}