const {
    body,
    validationResult
} = require('express-validator')

const validateCMS = () => {
    return [
        body('name', 'This field is required!').trim().notEmpty(),
        body('title', 'This field is required!').trim().notEmpty(),
        body('content', 'This field is required!').trim().notEmpty(),
    ]
}
const validatecms = (req, res, next) => {
    const errors = validationResult(req)
    console.log("req", req.body)
    if(errors.isEmpty()) {
        return next()
    }
    const extractedErrors = []
    errors.array({ onlyFirstError: true }).map(err => extractedErrors.push({ [err.param]: err.msg }));

    console.log("extractedErrors=>>>>>>>>>>>>>>>>>", extractedErrors)
    req.flash('validationError', extractedErrors);
    req.flash('form', req.body);
    if (req.params.id) {
        return res.redirect('/admin/cms/' + req.params.id)
    }
    return res.redirect('/admin/cms')
}
module.exports = {
    validateCMS,
    validatecms
}