Spaces:
Runtime error
Runtime error
Download apps/api/src/middleware/validate.ts from ClaraSu/llm-router: direct link, hf CLI and curl.
- Browser
- Download file 545 Bytes
-
https://huggingface.co/spaces/ClaraSu/llm-router/resolve/main/apps/api/src/middleware/validate.ts
- Command line
-
hf download hf://spaces/ClaraSu/llm-router/apps/api/src/middleware/validate.ts
-
curl -L -o validate.ts https://huggingface.co/spaces/ClaraSu/llm-router/resolve/main/apps/api/src/middleware/validate.ts
545 Bytes
| import type { ZodSchema } from 'zod' | |
| import type { Request, Response, NextFunction, RequestHandler } from 'express' | |
| export const validate = (schema: ZodSchema): RequestHandler => | |
| (req: Request, res: Response, next: NextFunction): void => { | |
| const result = schema.safeParse(req.body) | |
| if (!result.success) { | |
| const issues = result.error.issues ?? [] | |
| res.status(400).json({ | |
| error: issues[0]?.message ?? 'Validation failed', | |
| details: issues, | |
| }) | |
| return | |
| } | |
| req.body = result.data | |
| next() | |
| } | |