<commit_before>/*global exports, require */

////////////////////////////////////////////////////////////////////////////////
/// @brief Foxx schema helper
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2015 triAGENS GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
///     http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author <NAME>
/// @author Copyright 2015, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////

var _ = require('underscore');

function toJSONSchema(id, schema) {
  'use strict';
  var required = [],
    properties = {};


  if (schema) {
    if (
      typeof schema.toJSONSchema === 'function' &&
      schema.toJSONSchema !== toJSONSchema &&
      schema.toJSONSchema !== require('org/arangodb/foxx/model').Model.toJSONSchema
    ) {
      // allow overriding toJSONSchema.
      return schema.toJSONSchema(id);
    }
    if (!schema.isJoi) {
      // assume schema is a Model
      schema = schema.prototype.schema;
    }
    _.each(schema, function (attributeSchema, attributeName) {
      var description = attributeSchema.describe(),
        jsonSchema = {type: description.type},
        rules = description.rules,
        flags = description.flags;

      if (flags && flags.presence === 'required') {
        jsonSchema.required = true;
        required.push(attributeName);
      }

      if (
        jsonSchema.type === 'number' &&
          _.isArray(rules) &&
          _.some(rules, function (rule) {
            return rule.name === 'integer';
          })
      ) {
        jsonSchema.type = 'integer';
      }

      properties[attributeName] = jsonSchema;
    });
  }

  return {
    id: id,
    required: required,
    properties: properties
  };
}

exports.toJSONSchema = toJSONSchema;<commit_msg>Adjust toJSONSchema to better cope with joi schemas.<commit_after>/*global exports, require */

////////////////////////////////////////////////////////////////////////////////
/// @brief Foxx schema helper
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2015 triAGENS GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
///     http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author <NAME>
/// @author Copyright 2015, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////

var _ = require('underscore'),
  joi = require('joi');

function toJSONSchema(id, schema) {
  'use strict';
  var jsonSchema = {
    id: id,
    required: [],
    properties: {}
  };

  if (!schema) {
    return jsonSchema;
  }

  if (
    typeof schema.toJSONSchema === 'function' &&
    schema.toJSONSchema !== toJSONSchema &&
    schema.toJSONSchema !== require('org/arangodb/foxx/model').Model.toJSONSchema
  ) {
    // allow overriding toJSONSchema.
    return schema.toJSONSchema(id);
  }

  if (typeof schema === 'function') {
    // assume schema is a Model
    schema = schema.prototype.schema || {};
  }

  if (!schema.isJoi) {
    schema = joi.object().keys(schema);
  }

  var description = schema.describe();

  if (description.type !== 'object') {
    return jsonSchema;
  }

  _.each(schema.children, function (attributeDescription, attributeName) {
    var attributeSchema = {type: attributeDescription.type},
      rules = attributeDescription.rules,
      flags = attributeDescription.flags;

    if (flags && flags.presence === 'required') {
      attributeSchema.required = true;
      jsonSchema.required.push(attributeName);
    }

    if (
      attributeSchema.type === 'number' &&
        _.isArray(rules) &&
        _.some(rules, function (rule) {
          return rule.name === 'integer';
        })
    ) {
      attributeSchema.type = 'integer';
    }

    jsonSchema.properties[attributeName] = attributeSchema;
  });

  return jsonSchema;
}

exports.toJSONSchema = toJSONSchema;
{'fraction_non_alphanumeric': 0.1902458686013704, 'fraction_numerical': 0.0048367593712212815, 'mean_word_length': 2.14711477488903, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2757653', 'n_tokens_mistral': 1346, 'n_tokens_neox': 1283, 'n_words': 394}