Skip to content

feat: implement safe JSON parsing for localStorage with validation and error handling#330

Merged
calvadev merged 5 commits intoshopstr-eng:mainfrom
arnavkirti:fix/JSON-parse
Apr 6, 2026
Merged

feat: implement safe JSON parsing for localStorage with validation and error handling#330
calvadev merged 5 commits intoshopstr-eng:mainfrom
arnavkirti:fix/JSON-parse

Conversation

@arnavkirti
Copy link
Copy Markdown
Contributor

@arnavkirti arnavkirti commented Apr 4, 2026

Description

  • Introduce parseJsonWithFallback and getLocalStorageJson in utils/safe-json.ts to centralize typed, safe JSON parsing for localStorage, with optional validation and automatic key cleanup via removeOnError.

  • Migrate cart, discount, and Nostr localStorage usage (including relays, mints, tokens, history, signer, etc.) to these helpers so malformed or invalid JSON no longer crashes rendering and instead recovers via validators and safe defaults.

  • Broaden selected localStorage-backed types where needed while still validating shapes at the boundary, and add unit tests covering valid/malformed JSON, validator failures, key removal, and non-throwing recovery paths.

Resolved or fixed issue

none

Affirmation

For more details on what to include, see:

Bug Report Template
Feature Request Template
Documentation Issue Template
Security Issue Template

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 4, 2026

@arnavkirti is attempting to deploy a commit to the shopstr-eng Team on Vercel.

A member of the Team first needs to authorize it.

@GautamBytes
Copy link
Copy Markdown
Contributor

Hey will you replace the remaining raw JSON.parse in checkout-card with the new helper, also either add real shape validation for signer or narrow the PR description so it doesn’t claim recovery from invalid shapes there.

@arnavkirti
Copy link
Copy Markdown
Contributor Author

@GautamBytes I have replaced the remaining raw JSON.parse and added real shape validation for signer. Please give it a look.

@userAdityaa
Copy link
Copy Markdown
Contributor

Hey @arnavkirti, just a small suggestion, could you make the description a bit more concise and add some spacing between the points? It already has good formatting, but improving the spacing would make it much easier to read.

Copy link
Copy Markdown
Contributor

@kaihere14 kaihere14 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solid fix for a real crash vector raw JSON.parse on localStorage without a try/catch is a ticking bomb. The
abstraction makes sense.

A few things:

  • removeOnError silently deletes the key on any parse failure or validation mismatch. For a cart or discount
    key, that's destructive the user loses their data with no warning. At minimum log the error; ideally only
    remove on known corruption, not validator mismatches (which could be a schema version mismatch).
  • parseJsonWithFallback swallows all errors and returns the fallback. If the caller passes a wrong fallback
    type (e.g. [] when the data is an object), it silently proceeds with bad state. The generic typing helps but
    doesn't fully prevent this at runtime.
  • The nostr-helper-functions.ts change is a large diff for a "safe JSON" PR worth confirming that the signer
    type refactor is actually needed here or if it's scope creep.
  • getLocalStorageJson does an SSR guard (typeof window === "undefined") but returns the fallback silently.
    Fine for now, but if this ever gets called server-side intentionally, it'll be hard to debug.

Tests are good. LGTM with the removeOnError behavior reconsidered.

@arnavkirti
Copy link
Copy Markdown
Contributor Author

@kaihere14 Thank you for the review! I have addressed it in the latest commits.

@GautamBytes
Copy link
Copy Markdown
Contributor

LGTM Now!!

@kaihere14
Copy link
Copy Markdown
Contributor

@kaihere14 Thank you for the review! I have addressed it in the latest commits.

All good now

@calvadev calvadev merged commit 7405048 into shopstr-eng:main Apr 6, 2026
1 check failed
@arnavkirti arnavkirti deleted the fix/JSON-parse branch April 6, 2026 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants