title: Convert schema model nodejs to swagger
body: <p>I have the following schema model written for nodejs and mongo db, I have to make sure to convert the schema to the swagger format.</p>
  
  <p>As you can see from the diagram, I have a property called "actions" which is an array of strings, this is failing to convert it in the right way.</p>
  
  <p>The code is based on the following project:
  <a href="https://github.com/giddyinc/mongoose-to-swagger" rel="nofollow noreferrer">mongoose-to-swagger</a></p>
  
  <p>Correct way of how it should be converted:</p>
  
  <pre><code>"actions": {
            "type": "array",
            "items": {
              "type": "string",
              "enum": [
                "create",
                "read",
                "update",
                "delete"
              ]
            }
          }
  </code></pre>
  
  <p>Wrong way of converting it:</p>
  
  <pre><code>"actions": {
                  "type": "array",
                  "items": {
                    "properties": {//Remove
                      "actions": {//Remove
                        "type": "string",
                        "enum": [
                          "create",
                          "read",
                          "update",
                          "delete"
                        ]
                      }
                    }
                  }
                }
  </code></pre>
  
  <p>Schema:</p>
  
  <pre><code>const apiKeySchema = new Schema({
    owner: {
      type: Schema.Types.ObjectId,
      ref: 'User',
      required: true
    },
    apiKey: String,
    expiration: Date,
    active: {
      type: Boolean,
      default: false
    },
    scopes: [{
      scope: { type: String, enum: acl.scopes },
      actions: [{ type: String, enum: acl.actions }]
    }]
  }, {
    timestamps: true
  })
  const model = mongoose.model('ApiKey', apiKeySchema)
  
  export const schema = model.schema
  export default model
  </code></pre>
  
  <p>modelToSwagger:</p>
  
  <pre><code>'use strict';
  
  const adjustType = type =&gt; {
    if (type === undefined || type === null) return "Problem";
    let newType = (type === 'ObjectId' || type === 'ObjectID') ? 'object' : type;
    return newType.toLowerCase();
  };
  
  function documentModel(Model) {
    const obj = {
      title: Model.modelName,
      properties: {},
      required: []
    };
  
    const pathsToSchema = (parent, paths) =&gt; {
      Object.keys(paths).map(x =&gt; paths[x]).forEach(mongooseProp =&gt; {
        console.log(mongooseProp.instance, mongooseProp.path)
        if(mongooseProp.instance !== undefined) {
        parent[mongooseProp.path] = {};
          const modelProp = parent[mongooseProp.path];
  
          if (mongooseProp.instance === 'Array') {
            modelProp.type = 'array';
            modelProp.items = {properties: {}}
  
            if (mongooseProp.schema) {
              pathsToSchema(modelProp.items.properties, mongooseProp.schema.paths);
            } else if (mongooseProp.caster) {
              let ob = {
                caster: mongooseProp.caster,
                path: mongooseProp.caster.path,
              }
              pathsToSchema(modelProp.items.properties, ob);
            } else {
              modelProp.items = {type: 'object'};
            }
          } else if (mongooseProp.instance === 'Embedded') {
            modelProp.properties = {};
            modelProp.type = 'object';
            pathsToSchema(modelProp.properties, mongooseProp.schema.paths);
          } else if (mongooseProp.instance === 'ObjectID') {
            modelProp.type = 'string';
            modelProp.required = mongooseProp.isRequired || false;
          } else if (mongooseProp.instance === 'Date') {
            modelProp.type = 'string';
            modelProp.format = 'date-time';
            modelProp.required = mongooseProp.isRequired || false;
          } else if (mongooseProp.instance !== undefined) {
            modelProp.type = adjustType(mongooseProp.instance);
            modelProp.required = mongooseProp.isRequired || false;
          }
  
          // custom mongoose options
          if (mongooseProp.options) {
            if (mongooseProp.options.description) {
              modelProp.description = mongooseProp.options.description;
            }
          }
          if (mongooseProp.enumValues &amp;&amp; mongooseProp.enumValues.length) {
            modelProp.enum = mongooseProp.enumValues;
          }
          if (modelProp.required) {
            obj.required.push(mongooseProp.path);
          }
          delete modelProp.required;
        }
      });
    };
  
    pathsToSchema(obj.properties, Model.schema.paths);
  
    return obj;
  }
  
  documentModel.adjustType = adjustType;
  module.exports = documentModel;
  </code></pre>
  
  <p>Use:</p>
  
  <pre><code>import m2s from './modelToSwagger'
  let definitions = { ApiKeyModel: m2s(apiKey)}
  console.log(JSON.stringify({definitions}, null, 2))
  </code></pre>
  
  <p>Can you give me a hand?</p>