<commit_before>const querystring = require('querystring');
const formatError = require('../../lib/format-error');
const errorMessages = require('./user-error-messages');
const get = require('lodash/get');
const userFormData = require('./user-form-data');

const getValidationMessages = function(error) {
  var validationErrorMessages = {};

  if (error) {
    error = formatError(error);
    error.output.payload.validationErrors.forEach(errorObj => {
      validationErrorMessages[errorObj.key] = validationErrorMessages[errorObj.key] || [];

      if (errorMessages[errorObj.key] && errorMessages[errorObj.key][errorObj.type]) {
        validationErrorMessages[errorObj.key].push(errorMessages[errorObj.key][errorObj.type]);
      } else if (errorObj.message) {
        validationErrorMessages[errorObj.key].push(errorObj.message);
      }
    });
  }

  return validationErrorMessages;
};

module.exports = {
  userRegistration : (user, client, request, error) => {
    const payload = request.payload || {};
    return {
      client: client.serialize({strictOidc:true}),
      title: 'Sign Up',
      formAction: `/user/register?${querystring.stringify(request.query)}`,
      returnTo: request.query.login ? `/op/auth?${querystring.stringify(request.query)}` : request.query.redirect_uri,
      error: !!error,
      validationErrorMessages: error && error.isBoom ? getValidationMessages(error) : error,
      email: payload.email || ''
    }
  },

  userProfile: (user, client, request, error) => {
    let validationErrorMessages = {};
    if (error) {
      validationErrorMessages = getValidationMessages(error);<commit_msg>Fix error if object path doesn't exist<commit_after>const querystring = require('querystring');
const formatError = require('../../lib/format-error');
const errorMessages = require('./user-error-messages');
const get = require('lodash/get');
const userFormData = require('./user-form-data');

const getValidationMessages = function(error) {
  var validationErrorMessages = {};

  if (error) {
    error = formatError(error);
    get(error, 'output.payload.validationErrors', []).forEach(errorObj => {
      validationErrorMessages[errorObj.key] = validationErrorMessages[errorObj.key] || [];

      if (errorMessages[errorObj.key] && errorMessages[errorObj.key][errorObj.type]) {
        validationErrorMessages[errorObj.key].push(errorMessages[errorObj.key][errorObj.type]);
      } else if (errorObj.message) {
        validationErrorMessages[errorObj.key].push(errorObj.message);
      }
    });
  }

  return validationErrorMessages;
};

module.exports = {
  userRegistration : (user, client, request, error) => {
    const payload = request.payload || {};
    return {
      client: client.serialize({strictOidc:true}),
      title: 'Sign Up',
      formAction: `/user/register?${querystring.stringify(request.query)}`,
      returnTo: request.query.login ? `/op/auth?${querystring.stringify(request.query)}` : request.query.redirect_uri,
      error: !!error,
      validationErrorMessages: error && error.isBoom ? getValidationMessages(error) : error,
      email: payload.email || ''
    }
  },

  userProfile: (user, client, request, error) => {
    let validationErrorMessages = {};
    if (error) {
      validationErrorMessages = getValidationMessages(error);
{'fraction_non_alphanumeric': 0.14826021180030258, 'fraction_numerical': 0.0, 'mean_word_length': 4.165625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '4317198', 'n_tokens_mistral': 938, 'n_tokens_neox': 918, 'n_words': 163}