Add dynamic route matching and validation improvements
Introduces dynamic route parameter support in route path computation and schema matching. Adds a utility for sending validation errors, updates schema definitions to support header validation, and refactors server middleware to handle header, body, and query validation with improved error handling. Also adds a placeholder user route and updates dependencies to include 'path-to-regexp'.
This commit is contained in:
30
modules/utils.js
Normal file
30
modules/utils.js
Normal file
@@ -0,0 +1,30 @@
|
||||
const path = require("node:path")
|
||||
const Logger = require("./logger")
|
||||
const logger = Logger.createLogger(path.join(__dirname, ".."))
|
||||
|
||||
function sendValidationError(req, res, zodResult, type, path, errorConfig) {
|
||||
const ip = req.headers["x-forwarded-for"] || req.socket.remoteAddress
|
||||
logger.warn(`Validation failed for ${req.method.cyan} ${path.cyan.bold} (${type}) ` + `<IP:${ip}>`.bold, ["WEB", "yellow"])
|
||||
|
||||
const response = {
|
||||
success: false,
|
||||
message: errorConfig.message || `Validation failed in ${type}`,
|
||||
errors: zodResult.error.issues.map(e => ({
|
||||
field: e.path.join("."),
|
||||
message: e.message
|
||||
}))
|
||||
}
|
||||
|
||||
if (errorConfig) {
|
||||
const extras = { ...errorConfig }
|
||||
delete extras.status
|
||||
delete extras.message
|
||||
Object.assign(response, extras)
|
||||
}
|
||||
|
||||
return res.status(errorConfig.status || 400).json(response)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
sendValidationError
|
||||
}
|
||||
Reference in New Issue
Block a user