HEX
Server: nginx/1.24.0
System: Linux nowruzgan 6.8.0-57-generic #59-Ubuntu SMP PREEMPT_DYNAMIC Sat Mar 15 17:40:59 UTC 2025 x86_64
User: babak (1000)
PHP: 8.3.6
Disabled: NONE
Upload Files
File: /var/dev/nowruzgan/rest/node_modules/rttc/lib/reify.js
/**
 * Module dependencies
 */

var _ = require('lodash');
var buildSchemaIterator = require('./helpers/build-schema-iterator');


/**
 * reify()
 *
 * Given an rttc type schema, strip out generics to convert it into
 * a more specific type which contains no "ref", "json", {}, or [] types.
 * In other words, this makes a type schema "strict", and the result of
 * this function always passes `rttc.isSpecific()`.
 *
 * @param  {*} typeSchema
 * @return {*}
 */
module.exports = function reify (typeSchema) {

  // Configure type schema iterator and use it to recursively
  // collapse generic types.
  var schemaIterator = buildSchemaIterator(
    function onFacetDict(facetDictionary, parentKeyOrIndex, callRecursive){
      var newFacetDict = _.reduce(facetDictionary, function (memo, val, key) {
        var facet = callRecursive(val, key);
        // Don't include collapsed sub-examples.
        if (!_.isUndefined(facet)) {
          memo[key] = facet;
        }
        return memo;
      }, {});

      // Just in case all of the properties were collapsed, don't inadvertently
      // create another generic by returning an empty {}.
      if (_.isEqual({}, newFacetDict)) {
        return undefined;
      }

      return newFacetDict;
    },
    function onPatternArray(patternArray, parentKeyOrIndex, iterateRecursive){
      var pattern = iterateRecursive(patternArray[0], 0);

      // Don't include collapsed sub-examples.
      if (_.isUndefined(pattern)) {
        return undefined;
      }
      return [ pattern ];
    },
    function onGenericDict(schema, parentKeyOrIndex){
      return undefined;
    },
    function onGenericArray(schema, parentKeyOrIndex){
      return undefined;
    },
    function onOther(schema, parentKeyOrIndex){
      if (schema === 'json') {
        return undefined;
      }
      if (schema === 'ref') {
        return undefined;
      }
      return schema;
    }
  );

  // Run the iterator to get the reified type schema.
  return schemaIterator(typeSchema);

};