All files / ragchat-api/utils handleValidations.js

100% Statements 19/19
100% Branches 3/3
100% Functions 1/1
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 201x 1x 1x 1x 47x 47x 47x 47x 47x 47x 47x 47x 19x 19x 19x 19x 19x 19x 47x  
import { CustomError } from "./handleErrors.js";
 
// ### Validation function
export const validate = (schema) => (req, res, next) => {
  performance.mark('validateInput:start');
  try {
    // validate provided schema against request body
    schema.parse(req.body);
    // advance
    performance.mark('validateInput:end');
    next();
  } catch (error) {
    // name error
    error.name = 'zodError';
    error.statusCode = 400;
    // send error to global error handler
    next(error);
  }
};