-
Notifications
You must be signed in to change notification settings - Fork 428
Open
Description
When generating for objects that have patternProperties with the strictIndexSignatures option, json-schema-to-typescript does not honor the strictIndexSignatures option.
I traced this down to this area of code:
json-schema-to-typescript/src/parser.ts
Lines 407 to 427 in c826963
| if (schema.patternProperties) { | |
| // partially support patternProperties. in the case that | |
| // additionalProperties is not set, and there is only a single | |
| // value definition, we can validate against that. | |
| singlePatternProperty = !schema.additionalProperties && Object.keys(schema.patternProperties).length === 1 | |
| asts = asts.concat( | |
| map(schema.patternProperties, (value, key: string) => { | |
| const ast = parse(value, options, key, processed, usedNames) | |
| const comment = `This interface was referenced by \`${parentSchemaName}\`'s JSON-Schema definition | |
| via the \`patternProperty\` "${key}".` | |
| ast.comment = ast.comment ? `${ast.comment}\n\n${comment}` : comment | |
| return { | |
| ast, | |
| isPatternProperty: !singlePatternProperty, | |
| isRequired: singlePatternProperty || includes(schema.required || [], key), | |
| isUnreachableDefinition: false, | |
| keyName: singlePatternProperty ? '[k: string]' : key, | |
| } | |
| }), | |
| ) |
Where it returns the keyName as [k: string] in the singlePatternProperty case but doesn't update the keyName property of the ast, which is used by the strictIndexSignatures routine:
json-schema-to-typescript/src/generator.ts
Lines 156 to 158 in c826963
| if (options.strictIndexSignatures && ast.keyName === '[k: string]') { | |
| return `${type} | undefined` | |
| } |
I attempted a fix in this PR, here: #560
Example schema
{
"type": "object",
"properties": {
"maybe": {
"type": "string",
},
"pattern": {
"type": "object",
"properties": {
"maybe": {
"type": "string"
}
},
"patternProperties": {
"leaf|tree": {
"type": "array"
}
}
}
}
}Expected output
export interface Experiment {
maybe?: string;
pattern?: {
maybe?: string;
/**
* This interface was referenced by `undefined`'s JSON-Schema definition
* via the `patternProperty` "leaf|tree".
*/
[k: string]: unknown[] | undefined;
};
[k: string]: unknown | undefined;
}Actual output
export interface Experiment {
maybe?: string;
pattern?: {
maybe?: string;
/**
* This interface was referenced by `undefined`'s JSON-Schema definition
* via the `patternProperty` "leaf|tree".
*/
[k: string]: unknown[];
};
[k: string]: unknown | undefined;
}Related
Metadata
Metadata
Assignees
Labels
No labels