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

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

Open Jason3S opened this issue 1 year ago • 1 comments

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.

Jason3S avatar May 03 '24 09:05 Jason3S