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

Creating a new Schema adds empty required array #38

Closed Anand-Moghe closed 6 years ago

Anand-Moghe commented 6 years ago

I have a JSON instance like below:

{
  "a": [
    {
      "MyName": "Ron"
    },
    {
      "lastName": "Aron"
    }
  ]
}

When I generate the new schema of this JSON above it is created as below:

import jsonSchemaGenerator from 'generate-schema';
const mySchema = jsonSchemaGenerator.json("CustomLegend1", this.sectionSchemaInstance);
console.log(" ***** JSON Schema =  " + JSON.stringify(mySchema));

  {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "CustomLegend1",
    "type": "object",
    "properties": {
        "a": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "MyName": {
                        "type": "string"
                    },
                    "lastName": {
                        "type": "string"
                    }
                },
                "required": []
            }
        }
    }
 }

Why there are no required[] fields ("required": []) ?

nijikokun commented 6 years ago

Because you don't use a field in every array element:

{
  "a": [
    {
      "MyName": "Ron"
    },
    {
      "MyName": "Dave",
      "lastName": "Aron"
    }
  ]
}
Anand-Moghe commented 6 years ago

Oh! Thanks.