-
Notifications
You must be signed in to change notification settings - Fork 0
Add optional validate guard to loadFromStorage + tests #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add optional validate guard to loadFromStorage + tests #11
Conversation
…uthored-by: Jake Ruesink <[email protected]>
STA-7 [Major] Add runtime validation to storage loader (utils.loadFromStorage)
Requested by: Jake Ruesink Summary Base branch for PR: codegen-bot/persist-todos-localstorage-STA-2-1754688724 Code context
export function loadFromStorage<T>(key: string, fallback: T): T {
const storage = getStorage();
if (!storage) return fallback;
try {
const raw = storage.getItem(key);
if (!raw) return fallback;
return JSON.parse(raw) as T; // broad cast
} catch {
return fallback;
}
}
export function loadFromStorage<T>(
key: string,
fallback: T,
validate?: (value: unknown) => value is T
): T {
const storage = getStorage();
if (!storage) return fallback;
try {
const raw = storage.getItem(key);
if (!raw) return fallback;
const parsed = JSON.parse(raw);
if (validate && !validate(parsed)) return fallback;
return parsed as T;
} catch {
return fallback;
}
}
Acceptance criteria
References |
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the ✨ Finishing Touches🧪 Generate unit tests
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
|
🔍 Check Suite Status for PR #11
|
…' of github.com:lambda-curry/react-router-starter into codegen/sta-7-major-add-runtime-validation-to-storage-loader
2cfaab9
into
codegen-bot/persist-todos-localstorage-STA-2-1754688724
Requested by: Jake Ruesink
Summary
packages/utils/src/storage.tsloadFromStorage<T>so parsed values can be gated by a provided type guard.validateis provided and returns false, returnfallbackinstead of trusting JSON.parse blindly.Changes
loadFromStorageto acceptvalidate?: (value: unknown) => value is Tand apply it to the parsed value.packages/utils/src/__tests__/storage.test.tscovering:Integration notes
apps/todo-app/app/lib/todo-context.tsx. Current behavior remains unchanged if omitted.Relevant files
Compliance
💻 View my work • 👤 Initiated by
Jake Ruesink• About Codegen⛔ Remove Codegen from PR • 🚫 Ban action checks