<commit_before>/*
 * Copyright (c) 2013, 2014, <NAME>
 * All rights reserved.
 *
 * Licensed under the BSD 3-Clause License.
 */

/* global define */

define([
  'app/editor/collection/elements',
  'app/editor/model/element',
  'sulfur/schema',
  'sulfur/schema/element',
  'sulfur/schema/elements',
  'sulfur/schema/qname',
  'sulfur/schema/value/simple/uri',
  'sulfur/ui/model'
], function (
    ElementsCollection,
    ElementModel,
    Schema,
    Element,
    Elements,
    QName,
    UriValue,
    Model
) {

  'use strict';

  return Model.clone({

    attributes: {
      name: { default: '' },
      namespace: { default: '' },
      elements: { default: function () { return ElementsCollection.create() } }
    },

    _extract: function (schema) {
      var elements = schema.elements.toArray().map(function (e) {
        return ElementModel.createFromObject(e);
      });
      return {
        name: schema.qname.localName,
        namespace: schema.qname.namespaceURI,
        elements: ElementsCollection.create(elements)
      };
    }

  }).augment({

    _validate: function (errors) {
      Model.prototype._validate.call(this, errors);
      var name = this.get('name');
      if (name) {
        Element.isValidName(name) || (errors.name = "must be a valid NCName");
      } else {
        errors.name = "must not be empty";
      }
      var namespace = this.get('namespace');
      if (namespace) {
        UriValue.isValidLiteral(namespace) || (errors.namespace = "must be a valid URI");
      } else {
        errors.namespace = "must not be empty";
      }
    },

    _construct: function () {
      var elements = this.get('elements');
      if (elements.size === 0 || !elements.isValid()) {
        return;
      }
      elements = Elements.create(elements.items.map(function (m) {
        return m.object;
      }));
      var qname = QName.create(this.get('name'), this.get('namespace'));
      return Schema.create(qname, elements);
    }

  });

});
<commit_msg>fix schema model validation to consider an empty elements collection<commit_after>/*
 * Copyright (c) 2013, 2014, <NAME>
 * All rights reserved.
 *
 * Licensed under the BSD 3-Clause License.
 */

/* global define */

define([
  'app/editor/collection/elements',
  'app/editor/model/element',
  'sulfur/schema',
  'sulfur/schema/element',
  'sulfur/schema/elements',
  'sulfur/schema/qname',
  'sulfur/schema/value/simple/uri',
  'sulfur/ui/model'
], function (
    ElementsCollection,
    ElementModel,
    Schema,
    Element,
    Elements,
    QName,
    UriValue,
    Model
) {

  'use strict';

  return Model.clone({

    attributes: {
      name: { default: '' },
      namespace: { default: '' },
      elements: { default: function () { return ElementsCollection.create() } }
    },

    _extract: function (schema) {
      var elements = schema.elements.toArray().map(function (e) {
        return ElementModel.createFromObject(e);
      });
      return {
        name: schema.qname.localName,
        namespace: schema.qname.namespaceURI,
        elements: ElementsCollection.create(elements)
      };
    }

  }).augment({

    _validate: function (errors) {
      Model.prototype._validate.call(this, errors);
      var name = this.get('name');
      if (name) {
        Element.isValidName(name) || (errors.name = "must be a valid NCName");
      } else {
        errors.name = "must not be empty";
      }
      var namespace = this.get('namespace');
      if (namespace) {
        UriValue.isValidLiteral(namespace) || (errors.namespace = "must be a valid URI");
      } else {
        errors.namespace = "must not be empty";
      }
      var elements = this.get('elements');
      (elements.size === 0) && (errors.elements = true);
    },

    _construct: function () {
      var elements = this.get('elements');
      if (elements.size === 0 || !elements.isValid()) {
        return;
      }
      elements = Elements.create(elements.items.map(function (m) {
        return m.object;
      }));
      var qname = QName.create(this.get('name'), this.get('namespace'));
      return Schema.create(qname, elements);
    }

  });

});

{'fraction_non_alphanumeric': 0.14902339040270074, 'fraction_numerical': 0.0050639016156257535, 'mean_word_length': 2.5033783783783785, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3189149', 'n_tokens_mistral': 1295, 'n_tokens_neox': 1241, 'n_words': 274}