<commit_before>import { pick, isEmpty, isString, isArray, forEach, pickBy } from 'lodash';

const urlRegExp = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-]*)?\??(?:[\-\+=&;%@\.\w]*)#?(?:[\.\!\/\\\w]*))?)/g; // eslint-disable-line max-len

export default function validateQueryParamaters(req, res, next) {
  const validatedQueryParams = {};

  // validate strings (ISO date)
  const dateQueryStringPairs = pick(req.query, ['dateCreated', 'dateModified', 'datePublished']);
  const validatedDateQueryStringPairs = pickBy(dateQueryStringPairs, (value) => Date.parse(value));
  Object.assign(validatedQueryParams, validatedDateQueryStringPairs);

  // validate string (URL)
  const urlQueryStringPair = pick(req.query, 'codeRepository');
  const validatedUrlQueryStringPair = pickBy(urlQueryStringPair, (value) => urlRegExp.test(urlQueryParamPair.codeRepository));
  Object.assign(validatedQueryParams, validatedUrlQueryStringPair);

  // validate arrays
  const arrayQueryStringPairs = pick(req.query, ['repos', 'keywords']);
  const validatedArrayQueryStringPairs = pickBy(arrayQueryStringPairs, (value) => isArray(value) && !isEmpty(value));
  Object.assign(validatedQueryParams, validatedArrayQueryStringPairs);

  // validate general strings
  const strQueryStringPairs = pick(req.query,
    ['author', 'identifier', 'description', 'license', 'title', 'version', 'uploadedBy']);

  const validatedStringQueryParamPairs = pickBy(strQueryStringPairs, isString);
  Object.assign(validatedQueryParams, validatedStringQueryParamPairs);

  req.validatedQueryParams = validatedQueryParams;
  next();
}<commit_msg>Add some error handeling for invalid query param states<commit_after>import { pick, isEmpty, isString, isArray, forEach, pickBy, intersection } from 'lodash';

const urlRegExp = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-]*)?\??(?:[\-\+=&;%@\.\w]*)#?(?:[\.\!\/\\\w]*))?)/g; // eslint-disable-line max-len

export default function validateQueryParamaters(req, res, next) {
  const validReposToSearch = intersection(req.query.repos, ['figshare', 'dataone']);
  if (isEmpty(validReposToSearch)) {
    next(new Error('Repos query param must 1.) not be empty and 2.) must contain supported repository names'));
  }

  const validatedQueryParams = {};

  // validate strings (ISO date)
  const dateQueryStringPairs = pick(req.query, ['dateCreated', 'dateModified', 'datePublished']);
  const validatedDateQueryStringPairs = pickBy(dateQueryStringPairs, (value) => Date.parse(value));
  Object.assign(validatedQueryParams, validatedDateQueryStringPairs);

  // validate string (URL)
  const urlQueryStringPair = pick(req.query, 'codeRepository');
  const validatedUrlQueryStringPair = pickBy(urlQueryStringPair, (value) => urlRegExp.test(urlQueryParamPair.codeRepository));
  Object.assign(validatedQueryParams, validatedUrlQueryStringPair);

  // validate arrays
  const arrayQueryStringPairs = pick(req.query, ['repos', 'keywords']);
  const validatedArrayQueryStringPairs = pickBy(arrayQueryStringPairs, (value) => isArray(value) && !isEmpty(value));
  Object.assign(validatedQueryParams, validatedArrayQueryStringPairs);

  // validate general strings
  const strQueryStringPairs = pick(req.query,
    ['author', 'identifier', 'description', 'license', 'title', 'version', 'uploadedBy']);

  const validatedStringQueryParamPairs = pickBy(strQueryStringPairs, isString);
  Object.assign(validatedQueryParams, validatedStringQueryParamPairs);

  if (isEmpty(validatedQueryParams)) {
    next(new Error('No query param search criteria found'));
  }

  req.validatedQueryParams = validatedQueryParams;
  next();
}
{'fraction_non_alphanumeric': 0.187797147385103, 'fraction_numerical': 0.0036978341257263604, 'mean_word_length': 5.679012345679013, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6948023', 'n_tokens_mistral': 1241, 'n_tokens_neox': 1180, 'n_words': 221}