Skip to content

Reducing complexity below 20#3329

Merged
RobinTail merged 9 commits intomasterfrom
max-complexity-20
Apr 22, 2026
Merged

Reducing complexity below 20#3329
RobinTail merged 9 commits intomasterfrom
max-complexity-20

Conversation

@RobinTail
Copy link
Copy Markdown
Owner

@RobinTail RobinTail commented Apr 21, 2026

The goal is to establish complexity standards, reduce it for especially long function to make it more readable and well tested.

Summary by CodeRabbit

  • New Features

    • Added a configurable merge mode for schema flattening to control merge vs strict behavior.
  • Refactor

    • Reorganized schema-processing into clearer, reusable helpers for more predictable behavior and easier maintenance.
    • Exposed utility helpers for broader reuse.
  • Tests

    • Expanded unit tests to cover merge behavior, variant handling, property-name extraction, and example combinations.
  • Chores

    • ESLint tightened to enforce a cyclomatic complexity limit.

@RobinTail RobinTail added the refactoring The better way to achieve the same result label Apr 21, 2026
@pullfrog
Copy link
Copy Markdown
Contributor

pullfrog Bot commented Apr 21, 2026

Reviewed PR #3329. The extract-function refactoring of flattenIO into processAllOf, processVariants, and processPropertyNames is mechanically correct — each helper faithfully preserves the original behavior. The new MergeMode type alias and the complexity: ["error", 20] ESLint rule enforce the stated goal. Unit tests for each extracted helper and pullRequestExamples are thorough. Approved with no issues.

Task list (4/4 completed)
  • Read the full diff to understand changes
  • Read changed source files for full context
  • Analyze refactoring correctness and test coverage
  • Submit review

Pullfrog  | View workflow run | via Pullfrog | Using Claude Opus𝕏

@RobinTail RobinTail added the coverage Additional tests label Apr 21, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 21, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • ✅ Review completed - (🔄 Check again to review again)
📝 Walkthrough

Walkthrough

Refactored JSON Schema flattening by extracting schema-walking logic into exported helpers (processAllOf, processVariants, processPropertyNames) and a MergeMode type; exported several previously internal utilities; enabled an ESLint complexity rule for express-zod-api; added tests covering the new helpers and example generation. (46 words)

Changes

Cohort / File(s) Summary
ESLint Configuration
eslint.config.js
Added an explicit cyclomatic complexity rule scoped to express-zod-api/src/*.ts: "complexity": ["error", 20].
JSON Schema Helpers Refactor
express-zod-api/src/json-schema-helpers.ts
Added exported MergeMode and FlattenObjectSchema type aliases; exported previously internal utilities (isJsonObjectSchema, propsMerger, canMerge, nestOptional); added exported helpers processAllOf, processVariants, processPropertyNames; refactored flattenIO to delegate expansion/merging logic to these helpers while preserving prior behaviors (coerce vs throw modes).
Tests: json-schema-helpers
express-zod-api/tests/json-schema-helpers.spec.ts
Expanded test suite to cover newly exported helpers and behaviors: isJsonObjectSchema, propsMerger, canMerge, nestOptional, processAllOf, processVariants, processPropertyNames, and pullRequestExamples (including optionality, coerce/throw modes, propertyNames handling, and Cartesian example combinations).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • Accessing core types via z.core #2844 — Modifies express-zod-api/src/json-schema-helpers.ts, including changes around flattenIO signature and JSON Schema handling, closely related to these helper refactors.

Poem

🐰 I hopped through schemas, tidy and spry,
Split allOf and variants under moonlight sky.
I nudged MergeMode, made helpers sing,
Tests danced in pairs — examples took wing,
A rabbit’s small refactor — neat as a carrot pie. 🥕

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title directly and accurately reflects the main objective of the PR: reducing function complexity metrics to below a threshold of 20. This is substantiated by the eslint.config.js enabling the complexity rule with a limit of 20, and the refactoring of the json-schema-helpers.ts file extracting logic into smaller helper functions to achieve this goal.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ 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 max-complexity-20

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

@pullfrog pullfrog Bot left a comment

Choose a reason for hiding this comment

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

Reviewed — no issues found.

Task list (4/4 completed)
  • Read the full diff to understand changes
  • Read changed source files for full context
  • Analyze refactoring correctness and test coverage
  • Submit review

Pullfrog  | View workflow run | Using Claude Opus𝕏

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.

🧹 Nitpick comments (2)
express-zod-api/tests/json-schema-helpers.spec.ts (1)

131-140: Strengthen the "optional" assertion.

The test only checks flatRequired stays empty but doesn't verify that the key was still added to flat.properties. Adding that assertion would fully characterize the isOptional=true branch of processPropertyNames and guard against regressions that skip property derivation entirely.

Proposed tweak
       processPropertyNames(entry, flat, flatRequired, true);
+      expect(flat.properties).toHaveProperty("key");
       expect(flatRequired).toEqual([]);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@express-zod-api/tests/json-schema-helpers.spec.ts` around lines 131 - 140,
Update the test "should not add to required when optional" to also assert that
processPropertyNames still adds the property into flat.properties when
isOptional=true: after calling processPropertyNames(entry, flat, flatRequired,
true) add an assertion that flat.properties contains the key from
entry.propertyNames.const (e.g., expect(flat.properties).toHaveProperty("key"))
and also assert the property value matches the derived property definition
(e.g., equals the value processPropertyNames should produce for that property)
so the test verifies both presence and correct property derivation in addition
to flatRequired remaining empty.
express-zod-api/src/json-schema-helpers.ts (1)

32-71: Clean extraction — behavior preserved.

processAllOf, processVariants, and processPropertyNames faithfully mirror the previous inline logic while reducing flattenIO's complexity. The MergeMode type and default "coerce" in flattenIO keep backward compatibility.

One minor note: these helpers are now part of the module's public surface. If they're only intended for internal reuse/testing, consider a @internal JSDoc tag to signal that to consumers.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@express-zod-api/src/json-schema-helpers.ts` around lines 32 - 71, These
helper functions are now part of the module's public surface but are intended
for internal reuse/testing; add an `@internal` JSDoc tag to each helper to signal
that (place /** `@internal` */ immediately above processAllOf, processVariants,
and processPropertyNames declarations) so tooling and consumers know they are
internal-only; alternatively, if you truly want to hide them, remove the export
for those constants (processAllOf, processVariants, processPropertyNames)
instead of adding JSDoc.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@express-zod-api/src/json-schema-helpers.ts`:
- Around line 32-71: These helper functions are now part of the module's public
surface but are intended for internal reuse/testing; add an `@internal` JSDoc tag
to each helper to signal that (place /** `@internal` */ immediately above
processAllOf, processVariants, and processPropertyNames declarations) so tooling
and consumers know they are internal-only; alternatively, if you truly want to
hide them, remove the export for those constants (processAllOf, processVariants,
processPropertyNames) instead of adding JSDoc.

In `@express-zod-api/tests/json-schema-helpers.spec.ts`:
- Around line 131-140: Update the test "should not add to required when
optional" to also assert that processPropertyNames still adds the property into
flat.properties when isOptional=true: after calling processPropertyNames(entry,
flat, flatRequired, true) add an assertion that flat.properties contains the key
from entry.propertyNames.const (e.g.,
expect(flat.properties).toHaveProperty("key")) and also assert the property
value matches the derived property definition (e.g., equals the value
processPropertyNames should produce for that property) so the test verifies both
presence and correct property derivation in addition to flatRequired remaining
empty.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 53d2c67c-bde4-439f-8a35-cc5402703b21

📥 Commits

Reviewing files that changed from the base of the PR and between e15c551 and 0c4d4c3.

📒 Files selected for processing (3)
  • eslint.config.js
  • express-zod-api/src/json-schema-helpers.ts
  • express-zod-api/tests/json-schema-helpers.spec.ts

@coveralls-official
Copy link
Copy Markdown

coveralls-official Bot commented Apr 21, 2026

Coverage Status

coverage: 100.0%. remained the same — max-complexity-20 into master

Copy link
Copy Markdown
Owner Author

@RobinTail RobinTail left a comment

Choose a reason for hiding this comment

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

ok

@RobinTail RobinTail merged commit f6a6670 into master Apr 22, 2026
14 checks passed
@RobinTail RobinTail deleted the max-complexity-20 branch April 22, 2026 07:07
RobinTail added a commit that referenced this pull request Apr 22, 2026
Continues #3329 

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Refactor**
* Improved internal upload validation organization for clearer, more
maintainable checks.

* **Tests**
* Expanded test coverage for upload validation, including new cases for
missing and present fields.

* **Chores**
  * Adjusted linting complexity threshold to a stricter level.
  * Updated benchmarks to measure schema validation behavior.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

coverage Additional tests refactoring The better way to achieve the same result

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant