Download backend/src/middleware/validateRequest.js from archittmittal/Backend_PatientAppointment: direct link, hf CLI and curl.
- Browser
- Download file 1.4 kB
-
https://huggingface.co/spaces/archittmittal/Backend_PatientAppointment/resolve/main/backend/src/middleware/validateRequest.js
- Command line
-
hf download hf://spaces/archittmittal/Backend_PatientAppointment/backend/src/middleware/validateRequest.js
-
curl -L -o validateRequest.js https://huggingface.co/spaces/archittmittal/Backend_PatientAppointment/resolve/main/backend/src/middleware/validateRequest.js
1.4 kB
| /** | |
| * Validation Middleware factory | |
| * Usage: | |
| * router.post('/path', validateRequest(schema), handler) // validates body | |
| * router.get('/path', validateRequest(schema, 'query'), handler) // validates query | |
| */ | |
| function validateRequest(schema, source = 'body') { | |
| return (req, res, next) => { | |
| const data = source === 'query' ? req.query : (source === 'params' ? req.params : req.body); | |
| const { error, value } = schema.validate(data, { | |
| abortEarly: false, | |
| allowUnknown: true, | |
| stripUnknown: true | |
| }); | |
| if (error) { | |
| const errorMessage = error.details.map(detail => detail.message).join(', '); | |
| const err = new Error(errorMessage); | |
| err.statusCode = 400; | |
| err.code = 'VALIDATION_ERROR'; | |
| return next(err); | |
| } | |
| // Replace request data with validated/stripped values | |
| if (source === 'query') { | |
| Object.defineProperty(req, 'query', { | |
| get() { return value; }, | |
| configurable: true | |
| }); | |
| } else if (source === 'params') { | |
| Object.defineProperty(req, 'params', { | |
| get() { return value; }, | |
| configurable: true | |
| }); | |
| } else { | |
| req.body = value; | |
| } | |
| next(); | |
| }; | |
| } | |
| module.exports = validateRequest; | |