mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2026-01-25 14:14:06 +00:00
MASSIVE commit
- moved to typescript - axios rate limitmodule is busted, removed for now, do something else for that - sequelize-typescript - dotenv, not dotenv-flow - removed playlist details route types for API ton of minor fixes and improvements
This commit is contained in:
38
validators/index.ts
Normal file
38
validators/index.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { validationResult } from "express-validator";
|
||||
|
||||
import type { RequestHandler } from "express";
|
||||
|
||||
import { getNestedValuesString } from "../utils/jsonTransformer.ts";
|
||||
|
||||
import curriedLogger from "../utils/logger.ts";
|
||||
const logger = curriedLogger(import.meta.filename);
|
||||
|
||||
/** Refer: https://stackoverflow.com/questions/58848625/access-messages-in-express-validator */
|
||||
export const validate: RequestHandler = (req, res, next) => {
|
||||
const errors = validationResult(req);
|
||||
if (errors.isEmpty()) {
|
||||
return next();
|
||||
}
|
||||
|
||||
const extractedErrors: Record<string, string>[] = [];
|
||||
errors.array().forEach((err) => {
|
||||
if (err.type === "alternative") {
|
||||
err.nestedErrors.forEach((nestedErr) => {
|
||||
extractedErrors.push({
|
||||
[nestedErr.path]: nestedErr.msg,
|
||||
});
|
||||
});
|
||||
} else if (err.type === "field") {
|
||||
extractedErrors.push({
|
||||
[err.path]: err.msg,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
res.status(400).send({
|
||||
message: getNestedValuesString(extractedErrors),
|
||||
errors: extractedErrors,
|
||||
});
|
||||
logger.warn("invalid request", { extractedErrors });
|
||||
return null;
|
||||
};
|
||||
Reference in New Issue
Block a user