Skip to content

fix: support Zod 4 on the existing Koa 2 session API - #238

Open
saramjh wants to merge 1 commit into
koajs:masterfrom
saramjh:fix/zod4-koa2-compat-20261110
Open

saramjh wants to merge 1 commit into
koajs:masterfrom
saramjh:fix/zod4-koa2-compat-20261110

Conversation

@saramjh

@saramjh saramjh commented Nov 10, 2026 •

Copy link
Copy Markdown

Summary

Keep the existing Koa 2 middleware API while migrating the Zod 3-only function schemas to Zod 4.

  • Upgrade zod dependency from ^3.24.1 to ^4.6.7.
  • Convert ten z.function().args(...).returns(...) declarations to Zod 4 function({ input: [...], output: ... }) schemas, preserving validation intent.
  • Add two regression tests for valid and invalid callback options.
  • No Koa 3 upgrade, package rename, or middleware public API changes.

Motivation

Downstream Strapi 5 / Node 24 apps attempting to resolve their Zod dependency tree to patched Zod 4 fail when importing koa-session@7.0.2: the legacy function().args method is no longer available. Existing upstream PR #235 addresses Koa 3 and Zod 4 together. This patch is deliberately narrower for Koa 2 consumers.

Verification

  • Based on koajs/session master 968ae94 with Node.js 24.22.2.
  • npm test: 122 passing, including two new regression tests and lint.
  • tsc --noEmit: PASS.
  • The corresponding compiled runtime adaptation was separately exercised in a Strapi 5.61.0 / PostgreSQL 17 cloned-data test, with unchanged mobile public API JSON 30/30, successful admin build and synthetic auth.

Please run the supported upstream CI and review TypeScript function inference before release. This patch does not claim to resolve all downstream security vulnerabilities.

Related: #235 and strapi/strapi#28246

Summary by Sourcery

Enable Zod 4 compatibility for the existing Koa 2 session API without introducing public middleware changes.

Bug Fixes:

  • Update callback schema validation to support Zod 4 while preserving the existing Koa 2 session middleware API.

Enhancements:

  • Migrate session option function schemas from the Zod 3 API to the Zod 4 API without changing callback contracts.

Build:

  • Upgrade the zod dependency to version 4.6.7.

Tests:

  • Add regression coverage for accepting valid callback options and rejecting non-function values.

Summary by CodeRabbit

  • Compatibility
    • Updated the package’s validation support for the current Zod release, while keeping existing session option behavior unchanged.
  • Bug Fixes
    • Session options continue to accept function-valued callbacks and reject non-function values where callbacks are expected.
  • Tests
    • Added coverage for valid and invalid callback values.

@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Zod dependency range changes from ^3.24.1 to ^4.6.7. Callback validators in SessionOptions use z.function input and output configuration instead of chained .args() and .returns() calls. The declared callback types and existing optional and default settings remain unchanged. Tests verify that function-valued externalKey callbacks and genid pass validation, and that invalid externalKey callback values fail validation.

Merge Risk: Minimal · up to 7c424

No merge-blocking issue was established for the Zod upgrade.

Pre-merge checks: Passed 5

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly summarizes the main change: adding Zod 4 support while retaining the existing Koa 2 session API.
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
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.
Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 2 files. (1 skipped: 1 unsupported.)

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

package.json

ESLint skipped: missing config or dependency (missing-dependency). The ESLint configuration references a package that is not available in the sandbox.

src/index.ts

ESLint skipped: the matched ESLint configuration already failed (missing-dependency).

test/index.test.ts

ESLint skipped: the matched ESLint configuration already failed (missing-dependency).

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f9da73d7-b4ef-4635-9c47-131c2bd5fd48
Commits

Reviewing files that changed from the base of the PR and between 968ae94 and 7c42484.

Files selected for processing: 3

  • package.json
  • src/index.ts
  • test/index.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@sourcery-ai

sourcery-ai Bot commented Nov 10, 2026

Copy link
Copy Markdown

Reviewer's Guide

API

Migrate the SessionOptions callback schemas to Zod 4 while preserving the existing Koa 2 options API.

  • src/index.ts L111-114 (+8 small edits)

Configuration

Upgrade the Zod dependency to a version that supports the migrated function schemas.

  • package.json L38

Tests

Add regression coverage for accepted callback functions and rejected non-function callback options.

  • test/index.test.ts L1, L11-26

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai 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.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/index.ts" line_range="78" />
<code_context>
-    get: z.function()
-      .args(z.any())
-      .returns(z.string()),
+    get: z.function({ input: [ z.any() ], output: z.string() }),
     /**
      * set the external key
</code_context>
<issue_to_address>
**Session package fails to load**

When the package is imported, zod 4's `z.function(...)` creates a function factory, not a schema that can be embedded in `z.object`; `SessionOptions.parse({})` therefore encounters non-schema fields during module initialization and throws, so consumers cannot import the package.

Use Zod 4-compatible schemas for validating callback values in `SessionOptions`, rather than embedding function factories in the object shape.

Also at `src/index.ts:83`, `src/index.ts:94`, `src/index.ts:99`, `src/index.ts:103`, `src/index.ts:111`, `src/index.ts:114`, `src/index.ts:121`, `src/index.ts:133`, `src/index.ts:138`.
</issue_to_address>

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: src/index.ts:78


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/index.ts Resolved
@saramjh

saramjh commented Nov 10, 2026

Copy link
Copy Markdown
Author

CI follow-up: I investigated the failing Actions jobs after opening this PR.

The test suite itself reports 122 passing on the checked Node.js versions. The jobs then fail in the post-test package validation step (npm run prepublishOnly -> tshy && tshy-after && attw --pack), with:

error while checking file:
Cannot read properties of undefined (reading 'filename')

To check whether this patch introduced the failure, I created a separate pristine worktree at upstream master commit 968ae94, ran npm install and npm run prepublishOnly there (without any Zod 4 changes), and reproduced the same attw error and exit code 3.

The red CI status reflects a reproducible pre-existing package-validation failure, not failed session tests caused by this PR. I am keeping unrelated tooling changes out of this focused Koa 2 / Zod 4 compatibility patch; maintainer guidance would be appreciated.

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