Skip to content

fix: validate usePluginData return type against default value - #46

Merged
joshuapare merged 2 commits into
mainfrom
fix/plugin-data-validation
Mar 22, 2026
Merged

fix: validate usePluginData return type against default value#46
joshuapare merged 2 commits into
mainfrom
fix/plugin-data-validation

Conversation

@joshuapare

@joshuapare joshuapare commented Mar 22, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Adds runtime structural type validation to usePluginData before accepting stored values
  • The Go backend returns any from the data store, and the result as T cast provides no runtime safety — when stored JSON has a different type than expected (e.g. object instead of array), callers crash with errors like favorites.includes is not a function
  • New matchesShape helper checks array vs object vs primitive alignment against the default value; mismatches fall back to the default with a console warning

Test plan

  • Verify kubernetes plugin homecard loads without crashing when cluster_favorites key has corrupt/mismatched data in the store
  • Verify normal read/write flow for arrays, objects, and primitives still works correctly
  • Check console for warning messages when type mismatch occurs

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced plugin data safety with runtime format validation. When retrieved data doesn't match the expected format, the system logs a warning and returns the default value, preventing potential data errors.

The Go backend returns `any` from the data store, and the TypeScript
generic cast (`result as T`) provides no runtime safety. When stored
data has a different JSON type than expected (e.g. object instead of
array), callers crash with confusing errors like
"favorites.includes is not a function".

Add a `matchesShape` check that validates the structural type (array,
object, primitive) matches the default value before accepting the
stored result. Falls back to the default with a console warning on
mismatch.
@coderabbitai

coderabbitai Bot commented Mar 22, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@joshuapare has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 22 minutes and 59 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: ca2957fa-9f95-46d0-8dea-f8770a4d269d

📥 Commits

Reviewing files that changed from the base of the PR and between f5d9619 and f05577f.

📒 Files selected for processing (1)
  • packages/omniviewdev-runtime/src/hooks/data/usePluginData.ts
📝 Walkthrough

Walkthrough

Added runtime type validation to the usePluginData hook via a new matchesShape helper function. When retrieved plugin data fails structural validation against the expected type, the hook logs a warning and returns the default value instead of casting mismatched results.

Changes

Cohort / File(s) Summary
Runtime Type Validation
packages/omniviewdev-runtime/src/hooks/data/usePluginData.ts
Added matchesShape helper for runtime type checking (handles primitives, arrays, and objects). Updated useQuery queryFn to validate retrieved values and fallback to defaultValue with console warning on mismatch.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A shape-checker hops into the fray,
Validating types in a type-safe way,
When data misbehaves and won't align,
A gentle warning—return what's divine!
Runtime checks keep the bugs at bay! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding runtime validation to usePluginData's return type against the default value to ensure type safety.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/plugin-data-validation

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

CodeRabbit can use Trivy to scan for security misconfigurations and secrets in Infrastructure as Code files.

Add a .trivyignore file to your project to customize which findings Trivy reports.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/omniviewdev-runtime/src/hooks/data/usePluginData.ts`:
- Around line 17-23: The matchesShape function treats defaultValue === null as
an "object" because typeof null === 'object', causing non-null objects/arrays to
incorrectly match; update matchesShape to first check if defaultValue === null
and return value === null in that case so only null matches null, then keep the
existing array and object checks (referring to the matchesShape function) and
the final typeof comparison for primitives.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 02916349-22a0-46f8-aac5-abd31c354adc

📥 Commits

Reviewing files that changed from the base of the PR and between dd2bd55 and f5d9619.

📒 Files selected for processing (1)
  • packages/omniviewdev-runtime/src/hooks/data/usePluginData.ts

Comment thread packages/omniviewdev-runtime/src/hooks/data/usePluginData.ts
typeof null === 'object' caused null defaults to match any object or
array. Check for null explicitly before the object/array branches.
@joshuapare
joshuapare merged commit 3ccb641 into main Mar 22, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant