Merge pull request #28670 from overleaf/td-rename-validate-req

Rename validateReq to parseReq

GitOrigin-RevId: a935aaa3f89495e164ce5d10b0bd80436571440f
This commit is contained in:
Tim Down
2026-01-15 09:05:09 +00:00
committed by Copybot
parent 6e5e53f4e6
commit 64da16f0d1
23 changed files with 99 additions and 102 deletions
+34
View File
@@ -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 }