mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-14 14:13:43 +01:00
Merge pull request #28670 from overleaf/td-rename-validate-req
Rename validateReq to parseReq GitOrigin-RevId: a935aaa3f89495e164ce5d10b0bd80436571440f
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
// @ts-check
|
||||
const { ParamsError } = require('./Errors')
|
||||
|
||||
/**
|
||||
* @typedef {import('zod').ZodType} ZodType
|
||||
* @typedef {import('express').Request} Request
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @typedef {import('zod').output<T>} output<T>
|
||||
*/
|
||||
|
||||
/**
|
||||
* Parse and validate a request against a Zod schema
|
||||
*
|
||||
* @template {ZodType} T
|
||||
* @param {Request} req - The Express request object
|
||||
* @param {T} schema - The Zod schema to validate against
|
||||
* @returns {output<T>} The validated request object
|
||||
*/
|
||||
function parseReq(req, schema) {
|
||||
const parsed = schema.safeParse(req)
|
||||
if (parsed.success) {
|
||||
return parsed.data
|
||||
} else if (parsed.error.issues.some(issue => issue.path[0] === 'params')) {
|
||||
// Parts of the URL path failed to validate; throw a specific error
|
||||
throw new ParamsError('Invalid params').withCause(parsed.error)
|
||||
} else {
|
||||
throw parsed.error
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { parseReq }
|
||||
Reference in New Issue
Block a user