<commit_before>const validator =     require('validator');
const extend =        require('extend');
const settings =      require('./settings');
const log =           require('./log');
const utils =         require('./utils');
const convertMapTo =  require('./convertMapTo');
const rawValidation = require('./rawValidation');

/**
 * Validate a field in provided data {@link map} using the provided validation {@link settings}.
 *
 * @static
 * @method module:vulcanval.validateField
 *
 * @param  {String} fieldName - The field name in data map. If the {@link map} is nested,
 * the field name is set as in plain map. Ex: `{user: {name: 'romel'}}` will be `'user.name'`.
 * @param  {map} map - The data map (plain or nested).
 * @param  {settings} settings - The validation settings.
 *
 * @return {Boolean|String} If it is valid, `false` will be returned. Otherwise
 * there will be an string message describing the error.
 *
 * @example
 * const map = {
 *   name: 'Romel',
 *   age: 22,
 *   likesPumpkin: false
 * };
 *
 * const settings = {
 *   fields: [{
 *     name: 'name',
 *     required: true,
 *     validators: {
 *       isAlphanumeric: 'en-US',
 *       isLowercase: true
 *     }
 *   }, {
 *     name: 'age',
 *     validators: {
 *       isInt: { min: 1, max: 500 }
 *     }
 *   }]
 * };
 *
 * const nameResult = vulcanval.validateField('name', map, settings);
 * console.log(nameResult); // 'This field should only contain lowercase text.'
 *
 * const ageResult = vulcanval.validateField('age', map, settings);
 * console.log(ageResult); // false
 */
module.exports = function (fieldName, map, customSettings) {
  'use strict';

  if (typeof map !== 'object') {
    return log.error('second parameter (map) must be an object');
  }
  if (typeof customSettings !== 'object') {
    return log.error('third parameter (settings) must be an object');
  }

  customSettings = settings.extend(customSettings);

  if (customSettings.enableNestedMaps) {
    map = convertMapTo('plain', map);
  }

  // Creating the './utilityContext' for this validation.
  const context = {
    validator,
    get (name) {
      if (map[name]) {
        return map[name];
      } else {
        log.warn(`field "${name}" not found in map`);
      }
    }
  };

  const isValidField = rawValidation({
    field: {
      name: fieldName,
      value: map[fieldName]
    },
    settings: customSettings,
    context
  });

  return isValidField;
};<commit_msg>update: validateField<commit_after>const validator =     require('validator');
const extend =        require('extend');
const settings =      require('./settings');
const log =           require('./log');
const utils =         require('./utils');
const convertMapTo =  require('./convertMapTo');
const rawValidation = require('./rawValidation');

/**
 * Validate a field in provided data {@link map} using the provided validation {@link settings}.
 *
 * @static
 * @method module:vulcanval.validateField
 *
 * @param  {String} fieldName - The field name in data map. If the {@link map} is nested,
 * the field name is set as in plain map. Ex: `{user: {name: 'romel'}}` will be `'user.name'`.
 * @param  {map} map - The data map (plain or nested).
 *
 * @return {Boolean|String} If it is valid, `false` will be returned. Otherwise
 * there will be an string message describing the error.
 *
 * @example
 * const map = {
 *   name: 'Romel',
 *   age: 22,
 *   likesPumpkin: false
 * };
 *
 * const settings = {
 *   fields: [{
 *     name: 'name',
 *     required: true,
 *     validators: {
 *       isAlphanumeric: 'en-US',
 *       isLowercase: true
 *     }
 *   }, {
 *     name: 'age',
 *     validators: {
 *       isInt: { min: 1, max: 500 }
 *     }
 *   }]
 * };
 *
 * const nameResult = vulcanval.validateField('name', map, settings);
 * console.log(nameResult); // 'This field should only contain lowercase text.'
 *
 * const ageResult = vulcanval.validateField('age', map, settings);
 * console.log(ageResult); // false
 */
module.exports = function (fieldName, map) {
  'use strict';

  if (typeof map !== 'object') {
    return log.error('second parameter (map) must be an object');
  }

  if (this.settings.enableNestedMaps) {
    map = convertMapTo('plain', map);
  }

  this.settings.context.get = function (name) {
    if (map[name]) {
      return map[name];
    } else {
      log.warn(`field "${name}" not found in map`);
    }
  };

  return this.rawValidation(fieldName);
};
{'fraction_non_alphanumeric': 0.15280898876404495, 'fraction_numerical': 0.002696629213483146, 'mean_word_length': 2.9215859030837006, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4704204', 'n_tokens_mistral': 1370, 'n_tokens_neox': 1356, 'n_words': 413}