nijikokun / generate-schema

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

"required" fields only take into account the last 2 #32

Closed elgalu closed 6 years ago

elgalu commented 7 years ago

Thanks for the project! it got me started on JSON schema generation for exercising some form of contract testing.

The required fields logic seem to only take into account the last 2 examples?

How to reproduce

By providing this JSON test data:

[
    {
        "name": "leo"
    },
    {
        "name": "mike",
        "age": "30"
    },
    {
        "name": "laura",
        "age": "33"
    }
]

Run

generate-schema --json-schema data/sample-teams.json

Got

Got name and age fields as required.

    "required": [
      "name",
      "age"
    ]

Expected

Expected only the name field as required.

    "required": [
      "name"
    ]

I guess to improve it, would need to take a look at https://github.com/nijikokun/generate-schema/blob/edacf92cbc4242840770f9249b6cee40d5b36547/src/schemas/json.js#L41

elgalu commented 7 years ago

For now I switched to https://pypi.python.org/pypi/genson/ as it correctly generates the required fields.

nijikokun commented 7 years ago

@elgalu thanks for raising the issue, have planned a fix for this

nijikokun commented 6 years ago

Has been fixed as of 2.5.1:

> [{"name":"leo"},{"name":"mike","age":"30"},{"name":"laura","age":"33"}]
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "name": {
        "type": "string"
      },
      "age": {
        "type": "string"
      }
    },
    "required": [
      "name",
      "age"
    ]
  }
}
sshim-cb commented 4 years ago

Any updates? I don't get any required fields.