-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e109cb8
commit 1e64bc2
Showing
7 changed files
with
108 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { FastifyPluginAsync, FastifyReply, FastifyRequest } from "fastify"; | ||
import fp from "fastify-plugin"; | ||
import { InternalServerError, ValidationError } from "../errors/index.js"; | ||
import { z, ZodError } from "zod"; | ||
import { fromError } from "zod-validation-error"; | ||
|
||
const zodValidationPlugin: FastifyPluginAsync = async (fastify, _options) => { | ||
fastify.decorate( | ||
"zodValidateBody", | ||
async function ( | ||
request: FastifyRequest, | ||
_reply: FastifyReply, | ||
zodSchema: z.ZodTypeAny, | ||
): Promise<void> { | ||
try { | ||
await zodSchema.parseAsync(request.body); | ||
} catch (e: unknown) { | ||
if (e instanceof ZodError) { | ||
throw new ValidationError({ | ||
message: fromError(e).toString().replace("Validation error: ", ""), | ||
}); | ||
} else if (e instanceof Error) { | ||
request.log.error(`Error validating request body: ${e.toString()}`); | ||
throw new InternalServerError({ | ||
message: "Could not validate request body.", | ||
}); | ||
} | ||
throw e; | ||
} | ||
}, | ||
); | ||
}; | ||
|
||
const fastifyZodValidationPlugin = fp(zodValidationPlugin); | ||
export default fastifyZodValidationPlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters