Skip to content

Commit

Permalink
Correct schema for mapped object with other keys (#567)
Browse files Browse the repository at this point in the history
* allow-extra-props-where-pattern-properties-not-empty

* object mapped and other keys

* docs

* remove unnecessary changes

* remove unnecessary changes 2

* remove unnecessary changes 3

* remove unnecessary changes 4

* fix auto test

* fix comment

* 0.60.0

* Back to 0.59.0

---------

Co-authored-by: netanel-mce <[email protected]>
  • Loading branch information
netanel-mce and netanel-mce authored Aug 24, 2023
1 parent 3662786 commit f38327e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
11 changes: 11 additions & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,17 @@ interface SomeOtherDefinition {
```


## [numeric-keys-and-others](./test/programs/numeric-keys-and-others)

```ts
interface NumericKeysAndOthers {
[key: number]: number;
a: string;
b: boolean;
}
```


## [object-numeric-index](./test/programs/object-numeric-index)

```ts
Expand Down
5 changes: 5 additions & 0 deletions test/programs/numeric-keys-and-others/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface NumericKeysAndOthers {
[key: number]: number;
a: string;
b: boolean;
}
22 changes: 22 additions & 0 deletions test/programs/numeric-keys-and-others/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"patternProperties": {
"^[0-9]+$": {
"type": "number"
}
},
"properties": {
"a": {
"type": "string"
},
"b": {
"type": "boolean"
}
},
"required": [
"a",
"b"
],
"type": "object"
}
1 change: 1 addition & 0 deletions test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ describe("schema", () => {
assertSchema("array-empty", "MyEmptyArray");
assertSchema("map-types", "MyObject");
assertSchema("extra-properties", "MyObject");
assertSchema("numeric-keys-and-others", "NumericKeysAndOthers");
});

describe("string literals", () => {
Expand Down
13 changes: 9 additions & 4 deletions typescript-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,9 @@ export class JsonSchemaGenerator {
definition.patternProperties = {
[NUMERIC_INDEX_PATTERN]: this.getTypeDefinition(arrayType),
};
if (!!Array.from((<any>propertyType).members)?.find((member: [string]) => member[0] !== "__index")) {
this.getClassDefinition(propertyType, definition);
}
} else {
definition.type = "array";
if (!definition.items) {
Expand Down Expand Up @@ -1079,8 +1082,8 @@ export class JsonSchemaGenerator {
}
const indexSymbol: ts.Symbol = (<any>indexSignature.parameters[0]).symbol;
const indexType = this.tc.getTypeOfSymbolAtLocation(indexSymbol, node);
const isStringIndexed = indexType.flags === ts.TypeFlags.String;
if (indexType.flags !== ts.TypeFlags.Number && !isStringIndexed) {
const isIndexedObject = indexType.flags === ts.TypeFlags.String || indexType.flags === ts.TypeFlags.Number;
if (indexType.flags !== ts.TypeFlags.Number && !isIndexedObject) {
throw new Error(
"Not supported: IndexSignatureDeclaration with index symbol other than a number or a string"
);
Expand All @@ -1105,9 +1108,11 @@ export class JsonSchemaGenerator {
if (!def) {
def = this.getTypeDefinition(typ, undefined, "anyOf");
}
if (isStringIndexed) {
if (isIndexedObject) {
definition.type = "object";
definition.additionalProperties = def;
if (!Object.keys(definition.patternProperties || {}).length) {
definition.additionalProperties = def;
}
} else {
definition.type = "array";
if (!definition.items) {
Expand Down

0 comments on commit f38327e

Please sign in to comment.