vega / ts-json-schema-generator

Generate JSON schema from your Typescript sources
MIT License
1.45k stars 192 forks source link

RegExp types are now emitted, breaking schema validation (v2.1.1) #1945

Open Jason3S opened 6 months ago

Jason3S commented 6 months ago

Related to: #1921, #1927, and #1930

With the release of 2.1.0, I started seeing schema validation errors.

Below is an example where a field in an interface could be a string or a RegExp. Before 2.1, this worked because the generated just dropped the RegExp class.

Given the following interface:

export interface PatternAdjustment {
    /** Id of the Adjustment, i.e. `short-compound` */
    id: string;
    /** RegExp pattern to match */
    regexp: string | RegExp;
    /** The amount of penalty to apply. */
    penalty: number;
}

Output: v2.1.1

    "PatternAdjustment": {
      "additionalProperties": false,
      "properties": {
        "id": {
          "description": "Id of the Adjustment, i.e. `short-compound`",
          "markdownDescription": "Id of the Adjustment, i.e. `short-compound`",
          "type": "string"
        },
        "penalty": {
          "description": "The amount of penalty to apply.",
          "markdownDescription": "The amount of penalty to apply.",
          "type": "number"
        },
        "regexp": {
          "anyOf": [
            {
              "type": "string"
            },
            {
              "format": "regex", <-- Unknown format
              "type": "string"
            }
          ],
          "description": "RegExp pattern to match",
          "markdownDescription": "RegExp pattern to match"
        }
      },
      "required": [
        "id",
        "regexp",
        "penalty"
      ],
      "type": "object"
    },

Output: v2.0.1

    "PatternAdjustment": {
      "additionalProperties": false,
      "properties": {
        "id": {
          "description": "Id of the Adjustment, i.e. `short-compound`",
          "markdownDescription": "Id of the Adjustment, i.e. `short-compound`",
          "type": "string"
        },
        "penalty": {
          "description": "The amount of penalty to apply.",
          "markdownDescription": "The amount of penalty to apply.",
          "type": "number"
        },
        "regexp": {
          "description": "RegExp pattern to match",
          "markdownDescription": "RegExp pattern to match",
          "type": "string"
        }
      },
      "required": [
        "id",
        "regexp",
        "penalty"
      ],
      "type": "object"
    },

I'm going to try to work around this by creating a new type and using @hidden.

domoritz commented 6 months ago

What if you just have a regex?