prefer-allow-fields-middleware - ESLint plugin rules
This rule suggests using the allowFields middleware instead of writing to req.allowed directly.
Severity#
warn. This rule is enabled in the recommended preset.
What it Targets#
This rule targets the src/api/middlewares.ts file. It reports inline middleware functions inside defineMiddlewares that mutate req.allowed, whether by assignment or by calling .push().
The following code is reported by the rule:
Instead, use the allowFields middleware from @medusajs/framework/http:
Why it's Important
The allowFields middleware is the idiomatic way to expand the allowed fields on a route. It is more concise, self-documenting, and composable: several plugins or middlewares can each call allowFields on the same route without overwriting one another.
allowFields is available since Medusa v2.21.0. If the project runs an earlier version, keep the req.allowed.push(...) pattern and disable this rule.Learn more in the Allowed Fields in API Routes documentation.
Fixable#
This rule isn't auto-fixable.
Turn it Off#
To turn off this rule, set it to off in your ESLint configuration:
Or disable it for a single line using an inline comment: