Skip to content

Commit

Permalink
fix #426 (#569)
Browse files Browse the repository at this point in the history
* fix #426

* fix #426

---------

Co-authored-by: can <[email protected]>
  • Loading branch information
chatcan and can authored Sep 1, 2023
1 parent 4ee4b09 commit 3268f3b
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
11 changes: 11 additions & 0 deletions test/programs/string-template-literal/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
interface MyObject {
a: `@${string}`,
b: `@${number}`,
c: `@${bigint}`,
d: `@${boolean}`,
e: `@${undefined}`,
f: `@${null}`,
g: `${string}@`,
h: `${number}@`,
i: `${string}@${number}`,
}
57 changes: 57 additions & 0 deletions test/programs/string-template-literal/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"type": "object",
"properties": {
"a": {
"type": "string",
"pattern": "^@.*$"
},
"b": {
"type": "string",
"pattern": "^@[0-9]*$"
},
"c": {
"type": "string",
"pattern": "^@[0-9]*$"
},
"d": {
"enum": [
"@false",
"@true"
],
"type": "string"
},
"e": {
"type": "string",
"const": "@undefined"
},
"f": {
"type": "string",
"const": "@null"
},
"g": {
"type": "string",
"pattern": "^.*@$"
},
"h": {
"type": "string",
"pattern": "^[0-9]*@$"
},
"i": {
"type": "string",
"pattern": "^.*@[0-9]*$"
}
},
"additionalProperties": false,
"required": [
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i"
],
"$schema": "http://json-schema.org/draft-07/schema#"
}
4 changes: 4 additions & 0 deletions test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@ describe("schema", () => {
assertSchema("string-literals-inline", "MyObject");
});

describe("template string", () => {
assertSchema("string-template-literal", "MyObject");
});

describe("custom dates", () => {
assertSchema("custom-dates", "foo.Bar");
});
Expand Down
38 changes: 38 additions & 0 deletions typescript-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,44 @@ export class JsonSchemaGenerator {
if (!!Array.from((<any>propertyType).members)?.find((member: [string]) => member[0] !== "__index")) {
this.getClassDefinition(propertyType, definition);
}
} else if (propertyType.flags & ts.TypeFlags.TemplateLiteral) {
definition.type = "string";
// @ts-ignore
const {texts, types} = propertyType;
const pattern = [];
for (let i = 0; i < texts.length; i++) {
const text = texts[i];
const type = types[i];

if (i === 0) {
pattern.push(`^`);
}

if (type) {
if (type.flags & ts.TypeFlags.String) {
pattern.push(`${text}.*`);
}

if (type.flags & ts.TypeFlags.Number
|| type.flags & ts.TypeFlags.BigInt) {
pattern.push(`${text}[0-9]*`);
}

if (type.flags & ts.TypeFlags.Undefined) {
pattern.push(`${text}undefined`);
}

if (type.flags & ts.TypeFlags.Null) {
pattern.push(`${text}null`);
}
}


if (i === texts.length - 1) {
pattern.push(`${text}$`);
}
}
definition.pattern = pattern.join("");
} else {
definition.type = "array";
if (!definition.items) {
Expand Down

0 comments on commit 3268f3b

Please sign in to comment.