fix: apply additional data validator using a global middleware (#12194)

This commit is contained in:
Harminder Virk
2025-04-16 16:26:44 +05:30
committed by GitHub
parent b890263725
commit ee35f3ce90
9 changed files with 156 additions and 67 deletions
@@ -1,6 +1,4 @@
import zod from "zod"
import { defineMiddlewares } from "../define-middlewares"
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
describe("defineMiddlewares", function () {
test("define custom middleware for a route", () => {
@@ -20,38 +18,4 @@ describe("defineMiddlewares", function () {
],
})
})
test("should wrap additionalDataValidator to middleware", () => {
const req = {
body: {},
} as MedusaRequest
const res = {} as MedusaResponse
const nextFn = jest.fn()
const schema = {
brand_id: zod.string(),
}
const config = defineMiddlewares([
{
matcher: "/admin/products",
additionalDataValidator: schema,
},
])
expect(config).toMatchObject({
routes: [
{
matcher: "/admin/products",
middlewares: [expect.any(Function)],
},
],
})
config.routes?.[0].middlewares?.[0](req, res, nextFn)
expect(req.additionalDataValidator!.parse({ brand_id: "1" })).toMatchObject(
{
brand_id: "1",
}
)
})
})