All files / src/middleware validation.ts

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

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 731x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 9x 9x 15x 15x
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'),
    body('author').isMongoId().withMessage('Invalid user ID'),
];
 
export const validateCommentId = [
    param('comment_id').isMongoId().withMessage('Invalid comment ID'),
];
 
 
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('post_id').isMongoId().withMessage('Invalid post 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();
};