fix: Apply strict schema for all body and query parameters (#7624)
This commit is contained in:
1 parent
a39b542759
commit
d2e1e9f8c7
6 files changed
+22
-15
No files matched your search
@@ -174,21 +174,16 @@ medusaIntegrationTestRunner({
|
|||||||
|
|
||||||
describe("POST /admin/customers", () => {
|
describe("POST /admin/customers", () => {
|
||||||
it("should create a customer", async () => {
|
it("should create a customer", async () => {
|
||||||
const response = await api
|
const response = await api.post(
|
||||||
.post(
|
|
||||||
"/admin/customers",
|
"/admin/customers",
|
||||||
{
|
{
|
||||||
first_name: "newf",
|
first_name: "newf",
|
||||||
last_name: "newl",
|
last_name: "newl",
|
||||||
email: "new@email.com",
|
email: "new@email.com",
|
||||||
password: "newpassword",
|
|
||||||
metadata: { foo: "bar" },
|
metadata: { foo: "bar" },
|
||||||
},
|
},
|
||||||
adminHeaders
|
adminHeaders
|
||||||
)
|
)
|
||||||
.catch((err) => {
|
|
||||||
console.log(err)
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(response.status).toEqual(200)
|
expect(response.status).toEqual(200)
|
||||||
expect(response.data.customer).toEqual(
|
expect(response.data.customer).toEqual(
|
||||||
|
|||||||
@@ -448,7 +448,7 @@ medusaIntegrationTestRunner({
|
|||||||
|
|
||||||
it("list sales channels from the publishable api key with free text search filter", async () => {
|
it("list sales channels from the publishable api key with free text search filter", async () => {
|
||||||
const response = await api.get(
|
const response = await api.get(
|
||||||
`/admin/sales-channels?q=2&publishable_api_key=${pubKey1.id}`,
|
`/admin/sales-channels?q=2&publishable_key_id=${pubKey1.id}`,
|
||||||
adminHeaders
|
adminHeaders
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ medusaIntegrationTestRunner({
|
|||||||
})
|
})
|
||||||
|
|
||||||
const response = await api.get(
|
const response = await api.get(
|
||||||
`/admin/promotions/${createdPromotion.id}?fields=id,code&expand=`,
|
`/admin/promotions/${createdPromotion.id}?fields=id,code`,
|
||||||
adminHeaders
|
adminHeaders
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -32,6 +32,12 @@ export const AdminCreateFulfillment = z.object({
|
|||||||
labels: z.array(AdminCreateFulfillmentLabel),
|
labels: z.array(AdminCreateFulfillmentLabel),
|
||||||
order: z.object({}),
|
order: z.object({}),
|
||||||
order_id: z.string(),
|
order_id: z.string(),
|
||||||
|
shipping_option_id: z.string().optional(),
|
||||||
|
data: z.record(z.unknown()).optional().nullable(),
|
||||||
|
packed_at: z.coerce.date().optional().nullable(),
|
||||||
|
shipped_at: z.coerce.date().optional().nullable(),
|
||||||
|
delivered_at: z.coerce.date().optional().nullable(),
|
||||||
|
canceled_at: z.coerce.date().optional().nullable(),
|
||||||
metadata: z.record(z.unknown()).optional().nullable(),
|
metadata: z.record(z.unknown()).optional().nullable(),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ describe("zodValidator", () => {
|
|||||||
expect(errorMessage).toContain("Invalid request: Field 'id' is required")
|
expect(errorMessage).toContain("Invalid request: Field 'id' is required")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should allow for non-strict parsing", async () => {
|
it("should apply strict by default", async () => {
|
||||||
const schema = z.object({
|
const schema = z.object({
|
||||||
id: z.string(),
|
id: z.string(),
|
||||||
})
|
})
|
||||||
@@ -123,12 +123,12 @@ describe("zodValidator", () => {
|
|||||||
company: "Stark Industries",
|
company: "Stark Industries",
|
||||||
}
|
}
|
||||||
|
|
||||||
const validated = await zodValidator(schema, toValidate)
|
const errorMessage = await zodValidator(schema, toValidate).catch(
|
||||||
|
(e) => e.message
|
||||||
|
)
|
||||||
|
|
||||||
expect(JSON.stringify(validated)).toBe(
|
expect(errorMessage).toBe(
|
||||||
JSON.stringify({
|
"Invalid request: Unrecognized fields: 'name, company'"
|
||||||
id: "1",
|
|
||||||
})
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -113,8 +113,14 @@ export async function zodValidator<T>(
|
|||||||
zodSchema: z.ZodObject<any, any> | z.ZodEffects<any, any>,
|
zodSchema: z.ZodObject<any, any> | z.ZodEffects<any, any>,
|
||||||
body: T
|
body: T
|
||||||
): Promise<z.ZodRawShape> {
|
): Promise<z.ZodRawShape> {
|
||||||
|
let strictSchema = zodSchema
|
||||||
|
// ZodEffects doesn't support setting as strict, for all other schemas we want to enforce strictness.
|
||||||
|
if ("strict" in zodSchema) {
|
||||||
|
strictSchema = zodSchema.strict()
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return await zodSchema.parseAsync(body)
|
return await strictSchema.parseAsync(body)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof ZodError) {
|
if (err instanceof ZodError) {
|
||||||
throw new MedusaError(
|
throw new MedusaError(
|
||||||
|
|||||||
Reference in new issue
Block a user