fix: guard against non-string values in frontmatter aliases - #377
Conversation
When frontmatter aliases contain non-string values (null, numbers, objects), prepareFuzzySearch throws TypeError on .toLowerCase(). Apply type guard filter in both searchNotes() and convertToMetadata() to skip non-string array elements. Repro: a note with aliases: [null, "validAlias"] in frontmatter causes continuous console errors.
📝 WalkthroughWalkthroughVault alias arrays are now filtered to string values in fuzzy note searches and metadata conversion, preventing non-string entries from entering either output. ChangesAlias sanitization
Estimated code review effort: 1 (Trivial) | ~5 minutes Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/services/vault-service.ts`:
- Around line 547-551: Update the aliases conversion expression in the metadata
conversion flow to only wrap scalar values when typeof aliases is "string";
preserve filtering for arrays and return undefined for all other scalar values.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: cf1cd032-ecbc-49ea-850f-e26811d6677f
📒 Files selected for processing (1)
src/services/vault-service.ts
| aliases: Array.isArray(aliases) | ||
| ? aliases | ||
| ? aliases.filter((a) => typeof a === 'string') | ||
| : aliases | ||
| ? [aliases] | ||
| : undefined, |
There was a problem hiding this comment.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
Reject non-string scalar aliases during metadata conversion.
Array aliases are filtered, but a truthy scalar such as 123 is still returned as [123], violating NoteMetadata.aliases?: string[] and allowing invalid values downstream. Use typeof aliases === "string" for the scalar branch; otherwise return undefined.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/services/vault-service.ts` around lines 547 - 551, Update the aliases
conversion expression in the metadata conversion flow to only wrap scalar values
when typeof aliases is "string"; preserve filtering for arrays and return
undefined for all other scalar values.
RAIT-09
left a comment
There was a problem hiding this comment.
LGTM! — thanks for tracking down the root cause and fixing both spots. Sorry for the slow review, grad school has been keeping me busy.
I'll handle the remaining CodeRabbit nit and formatting after merging — nothing needed on your end.
…lizer Follow-up to RAIT-09#377: a truthy non-string scalar (e.g. aliases: 42) still leaked into NoteMetadata. Centralize frontmatter alias normalization in one helper used by both searchNotes and convertToMetadata.
Summary
Fix
TypeError: n.toLowerCase is not a functioncrash insearchNotes()when frontmatteraliasescontain non-string values.Root Cause
searchNotes()spreads thealiasesarray directly intoprepareFuzzySearchsearch fields. When an element is not a string (e.g.,null, a number, or a YAML object),prepareFuzzySearchcalls.toLowerCase()on it and throwsTypeError.Trigger in Reporter's Vault
The bug was triggered by template files with YAML syntax errors:
The trailing colon makes YAML parse this as
[{ "{ VALUE:英文名 }": null }]— an array containing objects, not strings. When the plugin scans these template files,prepareFuzzySearchreceives objects instead of strings and crashes.Fix
Add a runtime
typeof === "string"filter in both places that consume frontmatter aliases:searchNotes()— filtersaliasArraybefore spreading into fuzzy search fieldsconvertToMetadata()— filters the aliases array in the return valueThis is a defensive fix: it prevents crashes regardless of how non-string values enter the aliases array (malformed YAML, plugin-generated notes, etc.).
Reproduction
Create a note with frontmatter:
aliases: [null, "validAlias"]— console shows continuousTypeError.Verification