Skip to content

Commit

Permalink
allow-extra-props-where-pattern-properties-not-empty
Browse files Browse the repository at this point in the history
  • Loading branch information
netanel-mce committed Aug 21, 2023
1 parent 3662786 commit 8fb2651
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
3 changes: 3 additions & 0 deletions test/programs/numeric-keys/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface NumericKeys {
[key: number]: number;
}
9 changes: 9 additions & 0 deletions test/programs/numeric-keys/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"patternProperties": {
"^[0-9]+$": {
"type": "number"
}
},
"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", "NumericKeys", { noExtraProps: true });
});

describe("string literals", () => {
Expand Down
9 changes: 5 additions & 4 deletions typescript-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ export class JsonSchemaGenerator {
if (this.args.defaultProps) {
definition.defaultProperties = [];
}
if (this.args.noExtraProps && definition.additionalProperties === undefined) {
if (this.args.noExtraProps && definition.additionalProperties === undefined && !Object.keys(definition.patternProperties || {}).length) {
definition.additionalProperties = false;
}
if (this.args.propOrder) {
Expand Down Expand Up @@ -1378,9 +1378,6 @@ export class JsonSchemaGenerator {
} else if (typ.flags & ts.TypeFlags.Intersection) {
if (this.args.noExtraProps) {
// extend object instead of using allOf because allOf does not work well with additional properties. See #107
if (this.args.noExtraProps) {
definition.additionalProperties = false;
}

const types = (<ts.IntersectionType>typ).types;
for (const member of types) {
Expand All @@ -1398,6 +1395,10 @@ export class JsonSchemaGenerator {
definition.required = unique((definition.required || []).concat(other.required)).sort();
}
}

if ( definition.additionalProperties === undefined && !Object.keys(definition.patternProperties || {}).length) {
definition.additionalProperties = false;
}
} else {
this.getIntersectionDefinition(typ as ts.IntersectionType, definition);
}
Expand Down

0 comments on commit 8fb2651

Please sign in to comment.