All jobs

Fix "TypeError: expressValidator is not a function" After Upgrading express-validator

Yes. An agent bumps express-validator, reproduces the crash at app.use(expressValidator(...)), rewrites each req.checkBody route as a body() chain and opens a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent clones the repository into its own cloud sandbox and reads how validation is wired before changing the version. In an Express app like bradtraversy/nodekb, the knowledge-base app from the "Node.js & Express From Scratch" series, package.json asks for "express-validator": "^3.2.0", which resolves to 3.2.1, beside express 4, express-session, connect-flash, express-messages, mongoose 5, passport and pug; the only script is "start": "node app", and there is no test directory. app.js line 3 requires the package and line 49 mounts it as middleware, app.use(expressValidator({ errorFormatter: ... })), with a formatter that turns a.b parameter names into a[b]. The legacy API then appears in two route files: routes/users.js lines 21 to 28 chain six req.checkBody(...) calls (name, email twice, username, password, and a password2 that must equals the password) and read req.validationErrors(); routes/articles.js lines 16 to 20 check title and body the same way. views/layout.pug lines 35 to 37 render whatever is in errors as one alert per error.msg.

Then it reproduces the failure. On the pinned release, node app prints Server started on port 3000... and then throws a MongooseError because config/database.js reads process.env.MONGODB_URI from a .env file that is gitignored and absent in the sandbox; the agent records that as the baseline rather than the bug. After npm install express-validator@latest, 7.3.2 today, the same command dies before the server starts: TypeError: expressValidator is not a function at app.js:49:9. express-validator 6 removed the legacy req.checkBody middleware and its errorFormatter, so the package no longer exports a function to call.

The fix is the API the package has had since 6: the middleware block and its require leave app.js, each route imports { body, validationResult }, and the checks move into the route's middleware array, body('title', 'Title is required').notEmpty(), with the password2 rule written as a custom check that compares against req.body.password. The handler reads validationResult(req).array() and renders the form when the array is not empty. Each error object still carries msg, so layout.pug needs no change; the old param field is now path, which nothing in the views reads.

Because the app has no tests and no database in the sandbox, the agent proves the rewritten chains the direct way: it mounts one of them in a throwaway Express app, posts an empty title to it and shows the JSON that comes back, [{"type":"field","value":"","msg":"Title is required","path":"title","location":"body"}], then starts node app again and shows it reaching the same Mongoose connection error as before, which means the module graph loads with the new package.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Upgrade express-validator to the current release. First read package.json, app.js and every route file and list each place that uses the legacy API: the expressValidator middleware, req.checkBody, req.check, req.validationErrors and errorFormatter. Run `node app` on the pinned version and record what happens, then install the latest release and run it again, quoting the first failure with its file and line. Rewrite each route with the body() and validationResult() API, keeping every rule and message exactly as it was, and remove the middleware from app.js. Check that the views still read the fields the new error objects carry. Prove each rewritten chain with a direct request to a small Express app and quote the response. Open a pull request that quotes the failing and the fixed runs.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

What comes back is a pull request touching package.json, app.js, routes/users.js and routes/articles.js: the dependency moved to the current 7 release, the middleware block and its require deleted, and each form route carrying its rules as a body() array with validationResult(req).array() in the handler. The description lists the eight req.checkBody calls it replaced with their messages, quotes TypeError: expressValidator is not a function from app.js:49 on the new release, and shows the empty-title request answered with the Title is required error object.

A reviewer checks three things: that every message string survived the move, since the pug templates show them to users; that the password2 rule still compares against the submitted password rather than a fixed value; and that the run's baseline failure, the missing MONGODB_URI, is the same before and after, so the rewrite is isolated from the sandbox's lack of a database. The run's record holds both node app runs and the direct request, so the response shape can be read rather than assumed.

Run it on every pull request labelled dependencies

A dependency bot opens the bump that breaks this app, and its pull request is where the fix belongs. Add an automation to the repository's Project with a github trigger on pull_request.labeled, its labels filter set to dependencies, and this prompt. When the bot's express-validator bump is labelled, the run starts with the pull request event placed after the prompt, reproduces the crash on that branch and opens its own pull request with the route rewrite for you to merge ahead of the bump.

The trigger fires only for labels applied by an owner, member or collaborator, never for pull requests from forks, and an automation's runs are capped by its own max_runs_per_hour. A bump that touches a package the prompt does not name ends with a report and no code change.

The use case this job follows, with its automation and trigger: Upgrade dependencies weekly.

Why run it on Rush?

A major bump of a request-validation library fails at boot, not at install, and only on the server that mounts it. In its own sandbox the agent runs node app on both versions and quotes the exact line that throws, so the pull request shows the break instead of describing it, and every command with its output stays in the run's record.

The rewrite also has to be checked without a database, since the app's only script starts a server that needs MongoDB. The agent can stand up a throwaway Express app in the sandbox, post a bad body to the new chain and paste the response, which is more proof than a laptop run that stops at the connection error.

Frequently asked questions

Why did express-validator remove the middleware?

Version 6 dropped the legacy req.checkBody family and the expressValidator() middleware in favour of validation chains you put in each route's middleware array, so rules live next to the handler that needs them. From 6 on, the package exports body, query, param and validationResult and no longer a function to mount.

Will the error messages shown to users change?

No. Each body('field', 'message') chain carries the same message the checkBody call had, and validationResult(req).array() returns objects with a msg field, which is the one the pug layout reads. The one field that changed name, param to path, is not used by any view in this app.

Does the agent need MongoDB to prove the change?

Not for this fix. The crash happens while app.js loads, before any database call, so the proof is that node app gets past the middleware line on the new release, plus a direct request to the rewritten chain in a minimal Express app. Form flows end to end stay for a staging deploy with a real database.

What about the custom errorFormatter?

It only rewrote dotted parameter names into bracket form for the old error objects. The new API has no global formatter; if a route ever needs that shape, validationResult(req).formatWith() does it per route, and the agent says so in the description rather than adding one nothing reads.

More jobs like this: Upgrade dependencies weekly

Keep reading