nijikokun / generate-schema

🧞 Convert JSON Objects to MySQL, JSON Schema, Mongoose, Google BigQuery, Swagger, and more.
MIT License
1.04k stars 136 forks source link

mongoose mode doesn't descend into array definitions #36

Open elliott-beach opened 6 years ago

elliott-beach commented 6 years ago

Mongoose mode doesn't descend into arrays. An object within an array of will have type mixed, unlike in the default JSON mode which will infer the type of the object within the array. Is this intentional?

Repro: Using example.json:

{
  "array":[{
    "value1": "a",
    "value2": "b"
    }]
}

With the default mode (json):

β‹Š> ~/m/p/so-app generate-schema example.json                                                                                                  
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "array": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "value1": {
            "type": "string"
          },
          "value2": {
            "type": "string"
          }
        }
      }
    }
  }
}

With mongoose-mode:

β‹Š> ~/m/p/so-app generate-schema -m example.json                                                                                               
{
  "array": {
    "type": [
      "Mixed"
    ]
  }
}

I noticed this while idly investigating the asker's problem in this old StackOverflow question: https://stackoverflow.com/questions/39362065/how-to-create-a-mongoose-schema-from-json.