All files / src/middleware validation.ts

100% Statements 20/20
100% Branches 2/2
100% Functions 1/1
100% Lines 20/20

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 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88          7x       7x             7x       7x         7x           7x         7x         7x             7x       7x           7x       7x         7x           7x       7x       7x 86x 86x 10x   76x  
import {body, param, validationResult} from 'express-validator';
import {Request, Response, NextFunction} from "express";
import {handleError} from "../utils/handle_error";
 
 
export const validatePostId = [
    body('postId').isMongoId().withMessage('Invalid post ID'),
    ]
 
export const validateComment = [
    ...validatePostId,
    body('content').isString().isLength({ min: 1 }).withMessage('Content is required'),
];
 
 
 
export const validateCommentId = [
    param('commentId').isMongoId().withMessage('Invalid comment ID'),
];
 
export const validateCommentDataOptional = [
    ...validateCommentId,
    body('content').optional().isString().isLength({ min: 1 }).withMessage('Content is required'),
];
 
export const validateCommentData = [
    ...validateCommentDataOptional,
    body('content').notEmpty().withMessage('Content is required'),
];
 
 
export const validateEmailPassword = [
    body('email').optional().isEmail().withMessage('Invalid email format'),
    body('password').optional().isLength({ min: 6 }).withMessage('Password must be at least 6 characters long'),
    ];
 
export const validateUserDataOptional = [
    body('username').optional().notEmpty().withMessage('Username is required'),
    ...validateEmailPassword
];
 
export const validateUserRegister = [
    body('username').notEmpty().withMessage('Username is required'),
    body('email').notEmpty().withMessage('Email is required'),
    body('password').notEmpty().withMessage('Password is required'),
    ...validateUserDataOptional
] ;
 
export const validateUserId = [
    param('id').isMongoId().withMessage('Invalid user ID'),
];
 
export const validateLogin = [
    body('email').notEmpty().withMessage('Email is required'),
    body('password').notEmpty().withMessage('Password is required'),
    ...validateEmailPassword
];
 
export const validateRefreshToken = [
    body('refreshToken').notEmpty().withMessage('Refresh token is required').isString().withMessage('Refresh token must be a string'),
];
 
export const validatePostDataOptional = [
    body('title').optional().isString().isLength({ min: 1 }).withMessage('Title is required'),
    body('content').optional().isString().isLength({ min: 1 }).withMessage('Content is required'),
];
 
export const validatePostData = [
    body('title').notEmpty().withMessage('Title is required'),
    body('content').notEmpty().withMessage('Content is required'),
    ...validatePostDataOptional
];
 
export const validatePostIdParam = [
    param('postId').isMongoId().withMessage('Invalid post ID'),
];
 
export const validateRoomUserIds = [
    param('receiverUserId').isMongoId().withMessage('Invalid user ID'),
];
 
export const handleValidationErrors = (req: Request, res: Response, next: NextFunction) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
        return handleError({ errors: errors.array(), message: 'Validation failed' }, res);
    }
    next();
};