Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON schema generation on object with an array. #37

Open
Jaikant opened this issue Jul 5, 2023 · 1 comment
Open

JSON schema generation on object with an array. #37

Jaikant opened this issue Jul 5, 2023 · 1 comment

Comments

@Jaikant
Copy link

Jaikant commented Jul 5, 2023

The json schema generator does not generate schema the way I want it to.
For e.g. When I create the schema on the object : {c: ["a", "b", "c"]}

The schema created is as below:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "description": "",
  "type": "object",
  "properties": {
    "c": {
      "type": "array",
      "items": {
        "required": [
          
        ],
        "properties": {
          
        }
      },
      "value": [
        "a",
        "b",
        "c"
      ]
    }
  },
  "required": [
    "c"
  ]
}

In the above schema - the array c, has a default value of [“a”, “b”, “c”]

Instead my requirement is that, we should create the schema as below.

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "description": "",
  "type": "object",
  "properties": {
    "c": {
      "type": "array",
      "items": {
        "type": "string",
        "enum": [
          "a",
          "b",
          "c"
        ]
      }
    }
  },
  "required": [
    "c"
  ]
}
@Jaikant
Copy link
Author

Jaikant commented Jul 5, 2023

On testing with an array of objects: { c: [{a: 1}, {a: 2}, {a: 3}] }

The output is:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "description": "",
  "type": "object",
  "properties": {
    "c": {
      "type": "array",
      "uniqueItems": true,
      "minItems": 1,
      "items": {
        "required": [
          "a"
        ],
        "properties": {
          "a": {
            "type": "number"
          }
        }
      },
      "value": [
        {
          "a": 1
        },
        {
          "a": 2
        },
        {
          "a": 3
        }
      ]
    }
  },
  "required": [
    "c"
  ]
}

In the above output, I need the "items" to have a "type" of "object".

So it seems, the fix could be to include the "type" for array elements? If the array elements are primitives, it could skip adding the "required" and "properties"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant