ts-json-schema-generator icon indicating copy to clipboard operation
ts-json-schema-generator copied to clipboard

create schema uses `anyOf` instead of `allOf`

Open george-haroun opened this issue 1 year ago • 5 comments

Hello,

Below is an example where interface could be

export type ISchema = ({ foo: string } | { bar: string }) & { baz: string };

Output is

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "anyOf": [
    {
      "additionalProperties": false,
      "properties": {
        "baz": {
          "type": "string"
        },
        "foo": {
          "type": "string"
        }
      },
      "required": [
        "baz",
        "foo"
      ],
      "type": "object"
    },
    {
      "additionalProperties": false,
      "properties": {
        "bar": {
          "type": "string"
        },
        "baz": {
          "type": "string"
        }
      },
      "required": [
        "bar",
        "baz"
      ],
      "type": "object"
    }
  ],
  "definitions": {}
}

I am wondering why not using allOf instead since it's more efficient and easier to understand especially when using any schema validation lib so output could be such as

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "allOf": [
    {
      "additionalProperties": false,
      "properties": {
        "baz": {
          "type": "string"
        }
      },
      "required": [
        "baz"
      ],
      "type": "object"
    },
    {
      "anyOf": [
        {
          "additionalProperties": false,
          "properties": {
            "foo": {
              "type": "string"
            }
          },
          "required": [
            "foo"
          ],
          "type": "object"
        },
        {
          "additionalProperties": false,
          "properties": {
            "bar": {
              "type": "string"
            }
          },
          "required": [
            "bar"
          ],
          "type": "object"
        }
      ]
    }
  ],
  "definitions": {}
}

Do you guys have any clue on how to improve this ?

Thanks

george-haroun avatar Jul 01 '24 10:07 george-haroun