Skip to content

Commit

Permalink
fix: allow additional_data to be undefined or null (#9687)
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage authored Oct 21, 2024
1 parent fa15db4 commit 63fff01
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 16 deletions.
94 changes: 84 additions & 10 deletions packages/core/framework/src/http/__tests__/validate-body.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,102 @@ describe("validateAndTransformBody", () => {
it("should pass additionalDataValidator to validator factory", async () => {
let mockRequest = {
query: {},
body: {},
body: {
additional_data: {},
},
} as MedusaRequest

const mockResponse = {} as MedusaResponse
const nextFunction = jest.fn()

mockRequest.additionalDataValidator = zod.object({
brand_id: zod.number(),
})
mockRequest.additionalDataValidator = zod
.object({
brand_id: zod.number(),
})
.nullish()

const validatorFactory = (schema?: Zod.ZodObject<any, any>) => {
return schema ? createLinkBody().merge(schema) : createLinkBody()
const validatorFactory = (
schema?: Zod.ZodOptional<Zod.ZodNullable<Zod.ZodObject<any, any>>>
) => {
return schema
? createLinkBody().extend({
additional_data: schema,
})
: createLinkBody()
}

let middleware = validateAndTransformBody(validatorFactory)

await middleware(mockRequest, mockResponse, nextFunction)
expect(nextFunction).toHaveBeenCalledWith(
expect(nextFunction.mock.calls[0]).toEqual([
new MedusaError(
"invalid_data",
`Invalid request: Field 'brand_id' is required`
)
)
`Invalid request: Field 'additional_data, brand_id' is required`
),
])
})

it("should allow additional_data to be undefined", async () => {
let mockRequest = {
query: {},
body: {},
} as MedusaRequest

const mockResponse = {} as MedusaResponse
const nextFunction = jest.fn()

mockRequest.additionalDataValidator = zod
.object({
brand_id: zod.number(),
})
.nullish()

const validatorFactory = (
schema?: Zod.ZodOptional<Zod.ZodNullable<Zod.ZodObject<any, any>>>
) => {
return schema
? createLinkBody().extend({
additional_data: schema,
})
: createLinkBody()
}

let middleware = validateAndTransformBody(validatorFactory)

await middleware(mockRequest, mockResponse, nextFunction)
expect(nextFunction.mock.calls[0]).toEqual([])
})

it("should allow additional_data nested properties to be undefined", async () => {
let mockRequest = {
query: {},
body: {
additional_data: {},
},
} as MedusaRequest

const mockResponse = {} as MedusaResponse
const nextFunction = jest.fn()

mockRequest.additionalDataValidator = zod
.object({
brand_id: zod.number().optional(),
})
.nullish()

const validatorFactory = (
schema?: Zod.ZodOptional<Zod.ZodNullable<Zod.ZodObject<any, any>>>
) => {
return schema
? createLinkBody().extend({
additional_data: schema,
})
: createLinkBody()
}

let middleware = validateAndTransformBody(validatorFactory)

await middleware(mockRequest, mockResponse, nextFunction)
expect(nextFunction.mock.calls[0]).toEqual([])
})
})
4 changes: 2 additions & 2 deletions packages/core/framework/src/http/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NextFunction, Request, Response } from "express"
import { ZodObject } from "zod"
import { ZodNullable, ZodObject, ZodOptional } from "zod"

import {
FindConfig,
Expand Down Expand Up @@ -148,7 +148,7 @@ export interface MedusaRequest<Body = unknown>
* Custom validator to validate the `additional_data` property in
* requests that allows for additional_data
*/
additionalDataValidator?: ZodObject<any, any>
additionalDataValidator?: ZodOptional<ZodNullable<ZodObject<any, any>>>
}

export interface AuthContext {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/framework/src/http/utils/define-middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export function defineMiddlewares<
*/
if (additionalDataValidator) {
customMiddleware.push((req, _, next) => {
req.additionalDataValidator = zod.object(additionalDataValidator)
req.additionalDataValidator = zod
.object(additionalDataValidator)
.nullish()
next()
})
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/framework/src/http/utils/validate-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function validateAndTransformBody(
zodSchema:
| z.ZodObject<any, any>
| ((
customSchema?: z.ZodObject<any, any>
customSchema?: z.ZodOptional<z.ZodNullable<z.ZodObject<any, any>>>
) => z.ZodObject<any, any> | z.ZodEffects<any, any>)
): (
req: MedusaRequest,
Expand Down
6 changes: 4 additions & 2 deletions packages/medusa/src/api/utils/validators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { z, ZodEffects, ZodObject } from "zod"
import { z, ZodEffects, ZodNullable, ZodObject, ZodOptional } from "zod"

/**
* Wraps the original schema to a function to accept and merge
Expand All @@ -8,7 +8,9 @@ export const WithAdditionalData = <T extends ZodObject<any, any>>(
originalSchema: T,
modifyCallback?: (schema: T) => ZodObject<any, any> | ZodEffects<any, any>
) => {
return (additionalDataValidator?: ZodObject<any, any>) => {
return (
additionalDataValidator?: ZodOptional<ZodNullable<ZodObject<any, any>>>
) => {
let schema: ZodObject<any, any>

if (!additionalDataValidator) {
Expand Down

0 comments on commit 63fff01

Please sign in to comment.