Skip to content

Add option for failing on unknown JSON Schema type #471

Open

Description

@Winded

Currently, the generator defaults to type { [k: string]: unknown; } if JSON Schema type is not a valid type. It would be helpful to have an option to make this fail the generation, i.e. throw an error instead of using this default value.

Activity

  1. cesarvspr commented on Oct 2, 2022

    @cesarvspr

    @Winded would you please provide and example of schema that is translated to unknown typescript type?

  2. Winded commented on Oct 11, 2022

    @Winded
    Author

    I modified the example JSON to have invalid type for a field:

    {
      "title": "Person",
      "type": "object",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "asd"
        },
        "age": {
          "description": "Age in years",
          "type": "integer",
          "minimum": 0
        },
        "hairColor": {
          "enum": ["black", "brown", "blue"],
          "type": "string"
        }
      },
      "required": ["firstName", "lastName"]
    }

    This results in generated code:

    /* tslint:disable */
    /**
     * This file was automatically generated by json-schema-to-typescript.
     * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
     * and run json-schema-to-typescript to regenerate this file.
     */
    
    export interface Person {
      firstName: string;
      lastName: {
        [k: string]: unknown;
      };
      /**
       * Age in years
       */
      age?: number;
      hairColor?: "black" | "brown" | "blue";
      [k: string]: unknown;
    }
  3. tlahav commented on Oct 20, 2022

    @tlahav
    Contributor

    Hi, looking a bit at this request. Does it make sense to add a 'strict' mode in the options and throw exception if we have a case of 'UNKNOWN'? or would that be too heavy handed?

    I noticed there's also options.unknownAny. Should we still allow for those UNKNOWNs even in this new 'strict' mode?

  4. rishilifts commented on Oct 13, 2026

    @rishilifts

    I'd like to work on this. My proposed scope is an opt-in rejectInvalidTypes option: reject invalid explicit type values, but keep valid unconstrained schemas working. unknownAny would stay independent. I'd include CLI support and regression tests. Is that scope still wanted? I'll use AI assistance and disclose it in the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions