You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add option for failing on unknown JSON Schema type #471
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.
/* 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. */exportinterfacePerson{firstName: string;lastName: {[k: string]: unknown;};/** * Age in years */age?: number;hairColor?: "black"|"brown"|"blue";[k: string]: unknown;}
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?
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.
Activity
cesarvspr commentedon Oct 2, 2022
@Winded would you please provide and example of schema that is translated to unknown typescript type?
Winded commentedon Oct 11, 2022
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:
tlahav commentedon Oct 20, 2022
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?
rishilifts commentedon Oct 13, 2026
I'd like to work on this. My proposed scope is an opt-in
rejectInvalidTypesoption: reject invalid explicittypevalues, but keep valid unconstrained schemas working.unknownAnywould 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.