mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-09 17:28:26 +00:00
fix: Schema validation errors correctly returned as 422 (#2828)
* fix: Schema validation errors correctly returned as 422
This commit is contained in:
@@ -27,6 +27,7 @@ enum HttpStatusCodes {
|
|||||||
NotFound = 404,
|
NotFound = 404,
|
||||||
Unauthorized = 401,
|
Unauthorized = 401,
|
||||||
Forbidden = 403,
|
Forbidden = 403,
|
||||||
|
UnprocessableContent = 422,
|
||||||
// eslint-disable-next-line @typescript-eslint/no-shadow
|
// eslint-disable-next-line @typescript-eslint/no-shadow
|
||||||
InternalServerError = 500,
|
InternalServerError = 500,
|
||||||
GatewayTimeout = 504,
|
GatewayTimeout = 504,
|
||||||
@@ -66,9 +67,9 @@ export const fastifyErrHandler = fastifyPlugin(async (server: FastifyZodProvider
|
|||||||
error: error.name
|
error: error.name
|
||||||
});
|
});
|
||||||
} else if (error instanceof ZodError) {
|
} else if (error instanceof ZodError) {
|
||||||
void res.status(HttpStatusCodes.Unauthorized).send({
|
void res.status(HttpStatusCodes.UnprocessableContent).send({
|
||||||
requestId: req.id,
|
requestId: req.id,
|
||||||
statusCode: HttpStatusCodes.Unauthorized,
|
statusCode: HttpStatusCodes.UnprocessableContent,
|
||||||
error: "ValidationFailure",
|
error: "ValidationFailure",
|
||||||
message: error.issues
|
message: error.issues
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ export const DefaultResponseErrorsSchema = {
|
|||||||
401: z.object({
|
401: z.object({
|
||||||
requestId: z.string(),
|
requestId: z.string(),
|
||||||
statusCode: z.literal(401),
|
statusCode: z.literal(401),
|
||||||
message: z.any(),
|
message: z.string(),
|
||||||
error: z.string()
|
error: z.string()
|
||||||
}),
|
}),
|
||||||
403: z.object({
|
403: z.object({
|
||||||
@@ -54,6 +54,13 @@ export const DefaultResponseErrorsSchema = {
|
|||||||
details: z.any().optional(),
|
details: z.any().optional(),
|
||||||
error: z.string()
|
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({
|
500: z.object({
|
||||||
requestId: z.string(),
|
requestId: z.string(),
|
||||||
statusCode: z.literal(500),
|
statusCode: z.literal(500),
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ export type TApiErrors =
|
|||||||
requestId: string;
|
requestId: string;
|
||||||
error: ApiErrorTypes.ValidationError;
|
error: ApiErrorTypes.ValidationError;
|
||||||
message: ZodIssue[];
|
message: ZodIssue[];
|
||||||
statusCode: 401;
|
statusCode: 422;
|
||||||
}
|
}
|
||||||
| {
|
| {
|
||||||
requestId: string;
|
requestId: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user