<commit_before>import DS from 'ember-data';
import Ember from 'ember';

let { decamelize, capitalize } = Ember.String;

const INVALID_PROPERTY_TEST = /^The property '#\//;
const MISSING_REQUIRED_PROPERTY_TEST = /did not contain a required property/;
const VALIDATION_PROPERTY_MATCH = /The property '#\/([^']+)'.+/;

function humanize(property) {
  return capitalize(decamelize(property).replace(/_/ig, ' '));
}

function getMissingPropertyError(message) {
  // Example Error:
  // "The property '#/separateOrganizationalUnits' did not contain a required
  // property of 'implemented'"
  "The property '#/separateOrganizationalUnits' did not contain a required property of 'implemented'"
  const VALIDATION_ERROR_MATCH = /The property '#\/.+' (.+)/;
  const PATH_SUFFIX_MATCH = /.*did not contain a required property of '([^']+)'$/

  let initialPath = message.replace(VALIDATION_PROPERTY_MATCH, '$1')
                          .replace('/', '.');
  let pathSuffix = message.replace(PATH_SUFFIX_MATCH, '$1');
  let path = `${initialPath}.${pathSuffix}`;
  let error = 'is required';

  let propertyName = `${humanize(pathSuffix)} ${humanize(initialPath)}`;

  return { path, error, propertyName };
}

function getInvalidPropertyError(message) {
  // Example Error:
  // "The property '#/alertNotifications/enabledNotifications' did not contain
  // a minimum number of items 1"
  const VALIDATION_ERROR_MATCH = /The property '#\/.+' (.+)/;

  let path = message.replace(VALIDATION_PROPERTY_MATCH, '$1')
                          .replace('/', '.');
  let error = message.replace(VALIDATION_ERROR_MATCH, '$1')
                     .replace('did', 'does');
  let property = path.split('.').reverse()[0];
  let propertyName = humanize(property);

  return { path, error, propertyName };
}

let Attestation = DS.Model.extend({
  handle: DS.attr('string'),
  organizationUrl: DS.attr('string'),
  userUrl: DS.attr('string'),
  userName: DS.attr('string'),
  userEmail: DS.attr('string'),
  schemaId: DS.attr('string'),
  document: DS.attr({ defaultValue: {} }),
  createdAt: DS.attr('iso-8601-timestamp'),

  setUser(user) {
    let userUrl = user.get('data.links.self');
    let userName = user.get('name');
    let userEmail = user.get('email');

    this.setProperties({ userUrl, userName, userEmail });
  },

  validationErrors: Ember.computed('errors.[]', function() {
    let validationErrors = [];
    this.get('errors').forEach((error) => {
      if(MISSING_REQUIRED_PROPERTY_TEST.test(error.message)) {
        validationErrors.push(getMissingPropertyError(error.message));
      } else if(INVALID_PROPERTY_TEST.test(error.message)) {
        validationErrors.push(getInvalidPropertyError(error.message));
      }
    });
    return validationErrors;<commit_msg>assert user is actually a user<commit_after>import DS from 'ember-data';
import Ember from 'ember';
import User from './user';

let { decamelize, capitalize } = Ember.String;

const INVALID_PROPERTY_TEST = /^The property '#\//;
const MISSING_REQUIRED_PROPERTY_TEST = /did not contain a required property/;
const VALIDATION_PROPERTY_MATCH = /The property '#\/([^']+)'.+/;

function humanize(property) {
  return capitalize(decamelize(property).replace(/_/ig, ' '));
}

function getMissingPropertyError(message) {
  // Example Error:
  // "The property '#/separateOrganizationalUnits' did not contain a required
  // property of 'implemented'"
  "The property '#/separateOrganizationalUnits' did not contain a required property of 'implemented'"
  const VALIDATION_ERROR_MATCH = /The property '#\/.+' (.+)/;
  const PATH_SUFFIX_MATCH = /.*did not contain a required property of '([^']+)'$/

  let initialPath = message.replace(VALIDATION_PROPERTY_MATCH, '$1')
                          .replace('/', '.');
  let pathSuffix = message.replace(PATH_SUFFIX_MATCH, '$1');
  let path = `${initialPath}.${pathSuffix}`;
  let error = 'is required';

  let propertyName = `${humanize(pathSuffix)} ${humanize(initialPath)}`;

  return { path, error, propertyName };
}

function getInvalidPropertyError(message) {
  // Example Error:
  // "The property '#/alertNotifications/enabledNotifications' did not contain
  // a minimum number of items 1"
  const VALIDATION_ERROR_MATCH = /The property '#\/.+' (.+)/;

  let path = message.replace(VALIDATION_PROPERTY_MATCH, '$1')
                          .replace('/', '.');
  let error = message.replace(VALIDATION_ERROR_MATCH, '$1')
                     .replace('did', 'does');
  let property = path.split('.').reverse()[0];
  let propertyName = humanize(property);

  return { path, error, propertyName };
}

let Attestation = DS.Model.extend({
  handle: DS.attr('string'),
  organizationUrl: DS.attr('string'),
  userUrl: DS.attr('string'),
  userName: DS.attr('string'),
  userEmail: DS.attr('string'),
  schemaId: DS.attr('string'),
  document: DS.attr({ defaultValue: {} }),
  createdAt: DS.attr('iso-8601-timestamp'),

  setUser(user) {
    Ember.assert('user must be a User model', user instanceof User);

    let userUrl = user.get('data.links.self');
    let userName = user.get('name');
    let userEmail = user.get('email');

    this.setProperties({ userUrl, userName, userEmail });
  },

  validationErrors: Ember.computed('errors.[]', function() {
    let validationErrors = [];
    this.get('errors').forEach((error) => {
      if(MISSING_REQUIRED_PROPERTY_TEST.test(error.message)) {
        validationErrors.push(getMissingPropertyError(error.message));
      } else if(INVALID_PROPERTY_TEST.test(error.message)) {
        validationErrors.push(getInvalidPropertyError(error.message));
      }
    });
    return validationErrors;
{'fraction_non_alphanumeric': 0.15727948990435706, 'fraction_numerical': 0.003542330853701736, 'mean_word_length': 3.9491673970201577, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '483378', 'n_tokens_mistral': 1744, 'n_tokens_neox': 1719, 'n_words': 383}