<commit_before>var async = require('async');
var validator = require('validator/lib/validators');
var uuid = require('node-uuid');
var utils = require('prana').utils;

var field = module.exports = {};

/**
 * The field() hook.
 */
field.field = function(fields, callback) {
  var newFields = {};
  var application = this.application;

  newFields['id'] = {
    title: 'Identifier',
    description: 'A Universally unique identifier.',
    schema: {
      type: 'string'
    },
    beforeCreate: function(settings, item, next) {
      if (!(settings.name in item)) {
        // Generate an UUID v4.
        item[settings.name] = uuid.v4();
      }
      next();
    }
  };

  newFields['text'] = {
    title: 'Text',
    schema: function(settings) {
      var schema = {
        type: (settings.maxLength > 256) ? 'text' : 'string'
      };

      // Add max/min properties if set.
      ['maxLength', 'minLength'].map(function(what) {
        if (what in settings) {
          schema[what] = settings[what];
        }
      });

      return schema;
    },
    element: function(fieldSettings) {
      return  {
        // If options is set, use a select element instead of text one.
        type: 'options' in fieldSettings ? 'select' : 'text'
      };
    },
    validate: function(settings, item, next) {
      // Default minLenght to 1.
      settings.minLength = settings.minLength || 1;

      // Default maxLenght to 256.
      settings.maxLength = settings.maxLength || 256;

      next(null, !validator.notEmpty(item[settings.name]) || validator.len(item[settings.name].toString(), settings.minLength, settings.maxLength) || 'Value must have from ' + settings.minLength + ' to ' + settings.maxLength + ' characters.');
    }
  };

  newFields['number'] = {
    title: 'Number',
    schema: function(settings) {
      var schema = {
        type: (settings.decimals && settings.decimals > 0) ? 'float' : 'integer'
      };

      // Add max/min properties if set.
      ['max', 'min'].map(function(what) {
        if (what in settings) {
          schema[what] = settings[what];
        }
      });

      return schema;
    },
    element: 'number',
    validate: function(settings, item, next) {
      next(null, validator.isNumeric(item[settings.name].toString()) || 'Invalid number.');
    }
  };

  newFields['date'] = {
    title: 'Date',
    schema: 'date',
    element: 'date',
    validate: function(settings, item, next) {
      next(null, validator.isDate(item[settings.name].toString()) || 'Invalid date.');
    }
  };

  newFields['datetime'] = {
    title: 'Datetime',
    schema: 'datetime',
    element: 'datetime',
    validate: function(settings, item, next) {
      next(null, validator.isDate(item[settings.name].toString()) || 'Invalid date/time.');
    }
  };

  newFields['boolean'] = {
    title: 'Boolean',
    description: 'True or false, yes or no, on or off.',
    schema: 'boolean',
    element: 'checkbox'
  };

  newFields['email'] = {
    title: 'Email',
    schema: {
      type: 'string'
    },
    element: 'email',
    validate: function(settings, item, next) {
      // Email validator oddly returns the email itself, so need to convert to
      // boolean.
      next(null, new Boolean(validator.isEmail(item[settings.name].toString())) || 'Invalid email.');
    }
  };

  newFields['url'] = {
    title: 'URL',
    schema: {
      type: 'string',
      url: true
    },
    element: 'url',
    validate: function(settings, item, next) {
      next(null, validator.isUrl(item[settings.name].toString()) || 'Invalid URL.');
    }
  };
<commit_msg>Changed convert boolean operation when validate email.<commit_after>var async = require('async');
var validator = require('validator/lib/validators');
var uuid = require('node-uuid');
var utils = require('prana').utils;

var field = module.exports = {};

/**
 * The field() hook.
 */
field.field = function(fields, callback) {
  var newFields = {};
  var application = this.application;

  newFields['id'] = {
    title: 'Identifier',
    description: 'A Universally unique identifier.',
    schema: {
      type: 'string'
    },
    beforeCreate: function(settings, item, next) {
      if (!(settings.name in item)) {
        // Generate an UUID v4.
        item[settings.name] = uuid.v4();
      }
      next();
    }
  };

  newFields['text'] = {
    title: 'Text',
    schema: function(settings) {
      var schema = {
        type: (settings.maxLength > 256) ? 'text' : 'string'
      };

      // Add max/min properties if set.
      ['maxLength', 'minLength'].map(function(what) {
        if (what in settings) {
          schema[what] = settings[what];
        }
      });

      return schema;
    },
    element: function(fieldSettings) {
      return  {
        // If options is set, use a select element instead of text one.
        type: 'options' in fieldSettings ? 'select' : 'text'
      };
    },
    validate: function(settings, item, next) {
      // Default minLenght to 1.
      settings.minLength = settings.minLength || 1;

      // Default maxLenght to 256.
      settings.maxLength = settings.maxLength || 256;

      next(null, !validator.notEmpty(item[settings.name]) || validator.len(item[settings.name].toString(), settings.minLength, settings.maxLength) || 'Value must have from ' + settings.minLength + ' to ' + settings.maxLength + ' characters.');
    }
  };

  newFields['number'] = {
    title: 'Number',
    schema: function(settings) {
      var schema = {
        type: (settings.decimals && settings.decimals > 0) ? 'float' : 'integer'
      };

      // Add max/min properties if set.
      ['max', 'min'].map(function(what) {
        if (what in settings) {
          schema[what] = settings[what];
        }
      });

      return schema;
    },
    element: 'number',
    validate: function(settings, item, next) {
      next(null, validator.isNumeric(item[settings.name].toString()) || 'Invalid number.');
    }
  };

  newFields['date'] = {
    title: 'Date',
    schema: 'date',
    element: 'date',
    validate: function(settings, item, next) {
      next(null, validator.isDate(item[settings.name].toString()) || 'Invalid date.');
    }
  };

  newFields['datetime'] = {
    title: 'Datetime',
    schema: 'datetime',
    element: 'datetime',
    validate: function(settings, item, next) {
      next(null, validator.isDate(item[settings.name].toString()) || 'Invalid date/time.');
    }
  };

  newFields['boolean'] = {
    title: 'Boolean',
    description: 'True or false, yes or no, on or off.',
    schema: 'boolean',
    element: 'checkbox'
  };

  newFields['email'] = {
    title: 'Email',
    schema: {
      type: 'string'
    },
    element: 'email',
    validate: function(settings, item, next) {
      // Email validator oddly returns the email itself, so need to convert to
      // boolean.
      next(null, !!validator.isEmail(item[settings.name].toString()) || 'Invalid email.');
    }
  };

  newFields['url'] = {
    title: 'URL',
    schema: {
      type: 'string',
      url: true
    },
    element: 'url',
    validate: function(settings, item, next) {
      next(null, validator.isUrl(item[settings.name].toString()) || 'Invalid URL.');
    }
  };

{'fraction_non_alphanumeric': 0.16121430551705018, 'fraction_numerical': 0.0038813418353202105, 'mean_word_length': 2.598503740648379, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3861948', 'n_tokens_mistral': 2219, 'n_tokens_neox': 2184, 'n_words': 533}