Middleware validator using zod schema
This commit is contained in:
7
api/package-lock.json
generated
7
api/package-lock.json
generated
@@ -12,6 +12,7 @@
|
||||
"drizzle-orm": "^0.44.2",
|
||||
"drizzle-zod": "^0.8.2",
|
||||
"express": "^5.1.0",
|
||||
"lodash": "^4.17.21",
|
||||
"mysql2": "^3.14.1",
|
||||
"zod": "^3.25.74"
|
||||
},
|
||||
@@ -1697,6 +1698,12 @@
|
||||
"integrity": "sha512-Ks/IoX00TtClbGQr4TWXemAnktAQvYB7HzcCxDGqEZU6oCmb2INHuOoKxbtR+HFkmYWBKv/dOZtGRiAjDhj92g==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/lodash": {
|
||||
"version": "4.17.21",
|
||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
|
||||
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/long": {
|
||||
"version": "5.3.2",
|
||||
"resolved": "https://registry.npmjs.org/long/-/long-5.3.2.tgz",
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
"drizzle-orm": "^0.44.2",
|
||||
"drizzle-zod": "^0.8.2",
|
||||
"express": "^5.1.0",
|
||||
"lodash": "^4.17.21",
|
||||
"mysql2": "^3.14.1",
|
||||
"zod": "^3.25.74"
|
||||
},
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { z, ZodError } from 'zod';
|
||||
import { z, ZodError, ZodTypeAny } from 'zod';
|
||||
import { ZodObject } from 'zod/v4';
|
||||
|
||||
export function validateData(schema: z.ZodObject<any, any>) {
|
||||
export function validateData(schema: ZodObject) {
|
||||
return (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
schema.parse(req.body);
|
||||
next();
|
||||
} catch (error) {
|
||||
// console.log(error);
|
||||
if (error instanceof ZodError) {
|
||||
const errorMessages = error.errors.map((issue: any) => ({
|
||||
message: `${issue.path.join('.')} is ${issue.message}`,
|
||||
}));
|
||||
res.status(400).json({ error: 'Invalid data', details: errorMessages });
|
||||
} else {
|
||||
res.status(500).json({ error: 'Internal Server Error' });
|
||||
const er = error as ZodError
|
||||
res.status(400).json({ error: er.flatten()});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -6,17 +6,18 @@ import { listProducts,
|
||||
deleteProduct
|
||||
} from "./productsController";
|
||||
import { validateData } from "../../middlewares/validationMiddleware";
|
||||
import { z } from 'zod';
|
||||
import { z, ZodObject, ZodTypeAny } from 'zod/v4';
|
||||
import { createInsertSchema , createSelectSchema, createUpdateSchema } from 'drizzle-zod';
|
||||
import { productsTable } from "../../db/productsSchema";
|
||||
|
||||
const createProductSchema = z.object({
|
||||
name: z.string(),
|
||||
price: z.number({ message: "not a number you idiot."}),
|
||||
});
|
||||
// const createProductSchema = z.object({
|
||||
// name: z.string(),
|
||||
// price: z.number({ message: "not a number you idiot."}),
|
||||
// });
|
||||
|
||||
// const createProductSchema = createInsertSchema(productsTable);
|
||||
// type ProductType = z.infer<typeof createProductSchema>;
|
||||
|
||||
const createProductSchema = createInsertSchema(productsTable).omit({id: true});
|
||||
type ProductType = z.infer<typeof createProductSchema>;
|
||||
|
||||
const router = Router();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user