{"size": 1554, "ext": "js", "max_stars_repo_path": "server/middleware/validateInputs.js", "max_stars_repo_name": "shaolinmkz/Shoplink-Backend", "max_stars_repo_stars_event_min_datetime": "2019-08-19T17:32:20.000Z", "max_stars_repo_stars_event_max_datetime": "2019-08-23T09:00:33.000Z", "max_issues_repo_path": "server/middleware/validateInputs.js", "max_issues_repo_name": "kleva-j/Shoplink-Backend", "max_issues_repo_issues_event_min_datetime": "2019-08-22T22:42:43.000Z", "max_issues_repo_issues_event_max_datetime": "2019-09-06T13:21:48.000Z", "max_forks_repo_path": "server/middleware/validateInputs.js", "max_forks_repo_name": "kleva-j/Shoplink-Backend", "max_forks_repo_forks_event_min_datetime": "2019-08-18T16:26:07.000Z", "max_forks_repo_forks_event_max_datetime": "2019-08-18T16:26:07.000Z"}
{"max_stars_count": 2, "max_issues_count": 6, "max_forks_count": 1, "avg_line_length": 28.2545454545, "max_line_length": 79, "alphanum_fraction": 0.6846846847}
import { check } from 'express-validator';

/**
 * @description returns a starter template for express validator
 * @param {string} field
 * @returns {object} returns an object
 */
const checkTemplate = (field) => check(field).exists({
  checkNull: true,
  checkFalsy: true
});

/**
 * @description returns an express validator checker
 * @param {string} field
 * @returns {object} returns an object
 */
const subCheckTemplate = (field) => checkTemplate(field)
  .withMessage(`${field} is required`)
  .isString()
  .withMessage(`${field} should be a string`)
  .isLength({ min: 1 })
  .withMessage(`minimum character length for ${field} should be 1`)
  .isLength({ max: 50 })
  .withMessage(`maximum character length for ${field} should be 50`)
  .trim();

/**
 * @description Email validation function
 * @returns {object} returns a function
 */
export const validateEmail = checkTemplate('email')
  .withMessage('email is required')
  .isString()
  .withMessage('email should be a string')
  .matches(/^([A-z0-9]+)([._-]{0,1})([A-z0-9]+)@([A-z0-9-_.]+)\.([A-z]{2,3})$/)
  .withMessage('invalid email format')
  .trim();

/**
 * @description Password validation function
 * @returns {function} returns a function
 */
export const validatePassword = checkTemplate('password')
  .withMessage('password is required')
  .isString()
  .withMessage('password should be a string')
  .isLength({ min: 6 })
  .withMessage('minimum character length for password should be 6');

export default [
  subCheckTemplate('firstName'),
  subCheckTemplate('lastName')
];
