Skip to content

fix: add null check for object properties#1649

Merged
SaltyAom merged 1 commit intoelysiajs:mainfrom
aymaneallaoui:fix/has-elysia-meta-record-schema-crash
Jan 3, 2026
Merged

fix: add null check for object properties#1649
SaltyAom merged 1 commit intoelysiajs:mainfrom
aymaneallaoui:fix/has-elysia-meta-record-schema-crash

Conversation

@aymaneallaoui
Copy link
Copy Markdown
Contributor

@aymaneallaoui aymaneallaoui commented Jan 1, 2026

Bug: hasElysiaMeta crashes with TypeError: undefined is not an object (evaluating 'Object.keys(properties)') when schemas contain t.Record.

Why it happens:

  • t.Record(t.String(), t.String()) creates an object schema with patternProperties instead of properties
  • hasElysiaMeta assumes all type: "object" schemas have properties and iterates without checking
  • When recursively checking nested schemas (e.g., t.Union containing t.Array(t.Record(...))), it hits t.Record and crashes

The fix: Add a null guard before iterating:
if (!properties) return false

Summary by CodeRabbit

  • Bug Fixes
    • Improved schema validation robustness by adding proper handling for edge cases, preventing potential errors during certain validation operations.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Jan 1, 2026

Walkthrough

A guard clause was added to the hasElysiaMeta function in the object-type branch to check for falsy properties before iteration, preventing potential undefined reference errors during schema evaluation.

Changes

Cohort / File(s) Summary
Defensive guard addition
src/schema.ts
Added falsy check for properties in hasElysiaMeta function before iteration to prevent errors when properties is undefined

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A guard so keen, a check so bright,
Prevents undefined from spoiling the night,
With properties falsy, we pause and we wait,
The schema stays safe—what a wonderful fate! ✨

Pre-merge checks and finishing touches

✅ 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 a null check for object properties in the schema validation logic.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings

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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/schema.ts (1)

218-229: Logic issue: premature return inside loop.

Line 228 returns schema.elysiaMeta === meta inside the for loop, causing the function to exit after checking only the first property. This appears incorrect for two reasons:

  1. Early exit: The loop terminates after the first iteration regardless of whether more properties need checking
  2. Wrong target: It checks schema.elysiaMeta (already checked at line 192) instead of property.elysiaMeta

Compare with hasAdditionalProperties (line 115), which returns property.additionalProperties.

🔎 Proposed fix
 for (const key of Object.keys(properties)) {
   const property = properties[key]

+  if (property.elysiaMeta === meta) return true
+
   if (property.type === 'object') {
     if (hasElysiaMeta(meta, property)) return true
   } else if (property.anyOf) {
     for (let i = 0; i < property.anyOf.length; i++)
       if (hasElysiaMeta(meta, property.anyOf[i])) return true
   }
-
-  return schema.elysiaMeta === meta
 }
🧹 Nitpick comments (1)
src/schema.ts (1)

99-119: Consider adding similar guard in hasAdditionalProperties.

While line 103 handles patternProperties, an object schema without properties, patternProperties, or additionalProperties would still crash at line 105 when calling Object.keys(properties).

🔎 Suggested guard
 if (schema.type === 'object') {
   const properties = schema.properties as Record<string, TAnySchema>

   if ('additionalProperties' in schema) return schema.additionalProperties
   if ('patternProperties' in schema) return false
+  if (!properties) return false

   for (const key of Object.keys(properties)) {
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f751caf and b089e88.

📒 Files selected for processing (1)
  • src/schema.ts
🔇 Additional comments (1)
src/schema.ts (1)

216-216: LGTM - Guard prevents crash with Record schemas.

The null check correctly prevents the TypeError when properties is undefined (e.g., with t.Record which uses patternProperties). This is consistent with similar guards in hasType (line 176) and hasProperty (line 257).

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.

2 participants