File size: 781 Bytes
3a93fb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const { z } = require('zod');

// Validation schema for prediction request
const predictSchema = z.object({
  body: z.object({
    text: z.string().min(10, "Review must be at least 10 characters long").max(5000, "Review is too long"),
    sessionId: z.string().uuid("Invalid session ID").optional()
  })
});

const validate = (schema) => (req, res, next) => {
  try {
    schema.parse({
      body: req.body,
      query: req.query,
      params: req.params
    });
    next();
  } catch (err) {
    if (err instanceof z.ZodError) {
      return res.status(400).json({
        error: "Validation failed",
        details: err.errors.map(e => ({ path: e.path.join('.'), message: e.message }))
      });
    }
    next(err);
  }
};

module.exports = {
  predictSchema,
  validate
};