Skip to content

Reducing complexity to 18 (upload schema)#3331

Merged
RobinTail merged 2 commits intomasterfrom
max-complexity-18
Apr 22, 2026
Merged

Reducing complexity to 18 (upload schema)#3331
RobinTail merged 2 commits intomasterfrom
max-complexity-18

Conversation

@RobinTail
Copy link
Copy Markdown
Owner

@RobinTail RobinTail commented Apr 22, 2026

Continues #3329

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.

@RobinTail RobinTail added coverage Additional tests refactoring The better way to achieve the same result labels Apr 22, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 22, 2026

📝 Walkthrough

Walkthrough

This PR extracts a new internal helper isObjectOfUploadShape() used by the upload Zod schema, updates an ESLint complexity threshold (source/ez: 20 → 18), replaces a bench to compare two Zod schema variants, and adds unit tests for the new helper covering missing keys and non-object inputs.

Changes

Cohort / File(s) Summary
ESLint Configuration
eslint.config.js
Lowers the complexity rule threshold for source/ez from 20 to 18.
Upload Schema
express-zod-api/src/upload-schema.ts
Adds exported internal helper isObjectOfUploadShape(subject: unknown): boolean; replaces inline object-shape checks in the z.custom<UploadedFile>(...) predicate with a call to this helper.
Tests
express-zod-api/tests/upload-schema.spec.ts
Imports isObjectOfUploadShape and adds a test suite verifying: non-object inputs return false, deleting any required key returns false, and an object with all required keys returns true.
Benchmark
express-zod-api/bench/experiment.bench.ts
Removes Ramda-based set/union benchmarks; introduces two Zod schema factories (current() with inline predicate, featured() delegating to isObjectOfUploadShape) and benchmarks safeParse({}) for each.

Sequence Diagram(s)

(No sequence diagram generated — changes are focused on a helper extraction, tests, config, and benchmark adjustments without multi-component control-flow that benefits from a sequence diagram.)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 A little helper hops and peeks,

It counts the keys and guards the creeks.
Tests nibble clover, benchmarks hum,
Lint lines tighten — change has come.
Hooray, small hops make code feel plumb!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title mentions reducing complexity to 18 and references the upload schema, but the changeset involves multiple significant modifications beyond just the ESLint rule change, including refactored upload schema logic and expanded test coverage. Consider a more descriptive title that reflects the primary refactoring work, such as 'Extract isObjectOfUploadShape helper and increase ESLint complexity threshold' or similar, to better represent the full scope of changes.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
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.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch max-complexity-18

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.

@coveralls-official
Copy link
Copy Markdown

coveralls-official Bot commented Apr 22, 2026

Coverage Status

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

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: 1

🧹 Nitpick comments (1)
express-zod-api/bench/experiment.bench.ts (1)

47-55: Keep schema construction and early-exit input out of the benchmark hot path.

Right now each iteration measures z.custom() construction plus a parse that fails on the first missing key. Pre-create the schemas and parse a full upload-shaped object to measure the validation path being refactored.

Proposed benchmark adjustment
+  const validUpload = {
+    name: "avatar.jpg",
+    encoding: "utf-8",
+    mimetype: "image/jpeg",
+    data: Buffer.from("something"),
+    tempFilePath: "",
+    truncated: false,
+    size: 100500,
+    md5: "",
+    mv: () => {},
+  };
+
+  const currentSchema = current();
+  const featuredSchema = featured();
+
   bench("current", () => {
-    const one = current();
-    one.safeParse({});
+    currentSchema.safeParse(validUpload);
   });
 
   bench("featured", () => {
-    const one = featured();
-    one.safeParse({});
+    featuredSchema.safeParse(validUpload);
   });
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@express-zod-api/bench/experiment.bench.ts` around lines 47 - 55, The
benchmark currently constructs the schemas and parses an empty object inside
each bench iteration, measuring z.custom() creation and an early-failing parse;
move schema construction out of the hot path by calling current() and featured()
once before the bench declarations and reuse their returned schema objects
inside the bench callbacks (referencing current() and featured()), and replace
the empty input with a complete "upload-shaped" object that exercises the full
validation path so safeParse(...) validates actual upload data rather than
failing fast.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@express-zod-api/tests/upload-schema.spec.ts`:
- Around line 26-38: The test fixture `input` is currently inferred with all
required properties so `delete input[key]` violates strict mode; make `input` a
partial type before deleting (e.g., declare it as a Partial of the upload shape
or use Partial<typeof ...>) so properties can be removed safely, then perform
the `delete input[key]` and keep the call to
`expect(isObjectOfUploadShape(input)).toBe(false)`; update the declaration of
`input` in the test containing `isObjectOfUploadShape` and `key` to use a
Partial type.

---

Nitpick comments:
In `@express-zod-api/bench/experiment.bench.ts`:
- Around line 47-55: The benchmark currently constructs the schemas and parses
an empty object inside each bench iteration, measuring z.custom() creation and
an early-failing parse; move schema construction out of the hot path by calling
current() and featured() once before the bench declarations and reuse their
returned schema objects inside the bench callbacks (referencing current() and
featured()), and replace the empty input with a complete "upload-shaped" object
that exercises the full validation path so safeParse(...) validates actual
upload data rather than failing fast.
🪄 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: 86ec673b-75c6-460e-a604-a347214d6b11

📥 Commits

Reviewing files that changed from the base of the PR and between f6a6670 and d5c3253.

📒 Files selected for processing (4)
  • eslint.config.js
  • express-zod-api/bench/experiment.bench.ts
  • express-zod-api/src/upload-schema.ts
  • express-zod-api/tests/upload-schema.spec.ts

Comment thread express-zod-api/tests/upload-schema.spec.ts
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 (5/5 completed)
  • Read the PR diff and understand the changes
  • Read the changed source files for full context
  • Trace data flow and verify correctness of refactoring
  • Self-critique drafted comments
  • Submit review

Pullfrog  | View workflow run | Using Claude Opus𝕏

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 (5/5 completed)
  • Read the PR diff and understand the changes
  • Read the changed source files for full context
  • Trace data flow and verify correctness of refactoring
  • Self-critique drafted comments
  • 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.

♻️ Duplicate comments (1)
express-zod-api/tests/upload-schema.spec.ts (1)

26-38: ⚠️ Potential issue | 🟡 Minor

Keep the deletion fixture type-safe.

input is inferred with required properties, so delete input[key] can fail strict TypeScript checks. Mark the fixture partial before deleting. Based on learnings, strict: true enables strict null checking and related strict type-checking behavior.

Proposed fix
-      const input = {
+      const input: Partial<Record<typeof key, null>> = {
         name: null,
         encoding: null,
         mimetype: null,
         data: null,
         tempFilePath: null,
         truncated: null,
         size: null,
         md5: null,
         mv: null,
       };
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@express-zod-api/tests/upload-schema.spec.ts` around lines 26 - 38, The test
fixture `input` is currently inferred with all required properties so `delete
input[key]` violates strict TypeScript checks; change the fixture's type to a
partial type (e.g., declare `input` as Partial<typeof input>` or
`Partial<Upload>`/`Partial<Record<...>>`) or cast it to a writable partial
before deleting so the `delete input[key]` operation is type-safe; update the
test that uses `isObjectOfUploadShape` to use this partial-typed `input`
variable so the deletion compiles under `strict: true`.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@express-zod-api/tests/upload-schema.spec.ts`:
- Around line 26-38: The test fixture `input` is currently inferred with all
required properties so `delete input[key]` violates strict TypeScript checks;
change the fixture's type to a partial type (e.g., declare `input` as
Partial<typeof input>` or `Partial<Upload>`/`Partial<Record<...>>`) or cast it
to a writable partial before deleting so the `delete input[key]` operation is
type-safe; update the test that uses `isObjectOfUploadShape` to use this
partial-typed `input` variable so the deletion compiles under `strict: true`.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3994d00d-8ba3-4d70-b9f1-24bea0b07b88

📥 Commits

Reviewing files that changed from the base of the PR and between d5c3253 and 9394e1f.

📒 Files selected for processing (1)
  • express-zod-api/tests/upload-schema.spec.ts

@RobinTail RobinTail merged commit 5d5aa64 into master Apr 22, 2026
14 checks passed
@RobinTail RobinTail deleted the max-complexity-18 branch April 22, 2026 09:51
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