Spaces:
Runtime error
Runtime error
Download backend/src/middleware/validate.js from Akash-Dragon/EcoTracker-II: direct link, hf CLI and curl.
- Browser
- Download file 1.09 kB
-
https://huggingface.co/spaces/Akash-Dragon/EcoTracker-II/resolve/main/backend/src/middleware/validate.js
- Command line
-
hf download hf://spaces/Akash-Dragon/EcoTracker-II/backend/src/middleware/validate.js
-
curl -L -o validate.js https://huggingface.co/spaces/Akash-Dragon/EcoTracker-II/resolve/main/backend/src/middleware/validate.js
1.09 kB
| import { ZodError } from 'zod'; | |
| /** | |
| * Validate request body against a Zod schema | |
| * @param {import('zod').ZodSchema} schema | |
| */ | |
| export const validateBody = (schema) => (req, res, next) => { | |
| try { | |
| req.body = schema.parse(req.body); | |
| next(); | |
| } catch (err) { | |
| if (err instanceof ZodError) { | |
| const errors = err.errors.map((e) => ({ | |
| field: e.path.join('.'), | |
| message: e.message, | |
| })); | |
| return res.status(400).json({ | |
| success: false, | |
| error: 'Validation failed', | |
| details: errors, | |
| }); | |
| } | |
| next(err); | |
| } | |
| }; | |
| /** | |
| * Validate request params against a Zod schema | |
| * @param {import('zod').ZodSchema} schema | |
| */ | |
| export const validateParams = (schema) => (req, res, next) => { | |
| try { | |
| req.params = schema.parse(req.params); | |
| next(); | |
| } catch (err) { | |
| if (err instanceof ZodError) { | |
| return res.status(400).json({ | |
| success: false, | |
| error: 'Invalid parameters', | |
| details: err.errors.map((e) => ({ field: e.path.join('.'), message: e.message })), | |
| }); | |
| } | |
| next(err); | |
| } | |
| }; | |