Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

fix: Schema validation errors correctly returned as 422 (#2828)

McPizza7728a4793bdb
diff --git a/backend/src/server/plugins/error-handler.ts b/backend/src/server/plugins/error-handler.ts
index 0cbf30f097..920f17efbc 100644
--- a/backend/src/server/plugins/error-handler.ts
+++ b/backend/src/server/plugins/error-handler.ts
@@ -27,6 +27,7 @@ enum HttpStatusCodes {
NotFound = 404,
Unauthorized = 401,
Forbidden = 403,
+ UnprocessableContent = 422,
// eslint-disable-next-line @typescript-eslint/no-shadow
InternalServerError = 500,
GatewayTimeout = 504,
@@ -66,9 +67,9 @@ export const fastifyErrHandler = fastifyPlugin(async (server: FastifyZodProvider
error: error.name
});
} else if (error instanceof ZodError) {
- void res.status(HttpStatusCodes.Unauthorized).send({
+ void res.status(HttpStatusCodes.UnprocessableContent).send({
requestId: req.id,
- statusCode: HttpStatusCodes.Unauthorized,
+ statusCode: HttpStatusCodes.UnprocessableContent,
error: "ValidationFailure",
message: error.issues
});
diff --git a/backend/src/server/routes/sanitizedSchemas.ts b/backend/src/server/routes/sanitizedSchemas.ts
index 3fbbc60e31..e24c5db6ba 100644
--- a/backend/src/server/routes/sanitizedSchemas.ts
+++ b/backend/src/server/routes/sanitizedSchemas.ts
@@ -44,7 +44,7 @@ export const DefaultResponseErrorsSchema = {
401: z.object({
requestId: z.string(),
statusCode: z.literal(401),
- message: z.any(),
+ message: z.string(),
error: z.string()
}),
403: z.object({
@@ -54,6 +54,13 @@ export const DefaultResponseErrorsSchema = {
details: z.any().optional(),
error: z.string()
}),
+ // Zod errors return a message of varying shapes and sizes, so z.any() is used here
+ 422: z.object({
+ requestId: z.string(),
+ statusCode: z.literal(422),
+ message: z.any(),
+ error: z.string()
+ }),
500: z.object({
requestId: z.string(),
statusCode: z.literal(500),
diff --git a/frontend/src/hooks/api/types.ts b/frontend/src/hooks/api/types.ts
index 909e03a981..b387d33d17 100644
--- a/frontend/src/hooks/api/types.ts
+++ b/frontend/src/hooks/api/types.ts
@@ -54,7 +54,7 @@ export type TApiErrors =
requestId: string;
error: ApiErrorTypes.ValidationError;
message: ZodIssue[];
- statusCode: 401;
+ statusCode: 422;
}
| {
requestId: string;