Skip to content

Commit 3268f3b

Browse files
chatcancan
and
can
authored
* fix YousefED#426 * fix YousefED#426 --------- Co-authored-by: can <[email protected]>
1 parent 4ee4b09 commit 3268f3b

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
interface MyObject {
2+
a: `@${string}`,
3+
b: `@${number}`,
4+
c: `@${bigint}`,
5+
d: `@${boolean}`,
6+
e: `@${undefined}`,
7+
f: `@${null}`,
8+
g: `${string}@`,
9+
h: `${number}@`,
10+
i: `${string}@${number}`,
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"a": {
5+
"type": "string",
6+
"pattern": "^@.*$"
7+
},
8+
"b": {
9+
"type": "string",
10+
"pattern": "^@[0-9]*$"
11+
},
12+
"c": {
13+
"type": "string",
14+
"pattern": "^@[0-9]*$"
15+
},
16+
"d": {
17+
"enum": [
18+
"@false",
19+
"@true"
20+
],
21+
"type": "string"
22+
},
23+
"e": {
24+
"type": "string",
25+
"const": "@undefined"
26+
},
27+
"f": {
28+
"type": "string",
29+
"const": "@null"
30+
},
31+
"g": {
32+
"type": "string",
33+
"pattern": "^.*@$"
34+
},
35+
"h": {
36+
"type": "string",
37+
"pattern": "^[0-9]*@$"
38+
},
39+
"i": {
40+
"type": "string",
41+
"pattern": "^.*@[0-9]*$"
42+
}
43+
},
44+
"additionalProperties": false,
45+
"required": [
46+
"a",
47+
"b",
48+
"c",
49+
"d",
50+
"e",
51+
"f",
52+
"g",
53+
"h",
54+
"i"
55+
],
56+
"$schema": "http://json-schema.org/draft-07/schema#"
57+
}

test/schema.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,10 @@ describe("schema", () => {
387387
assertSchema("string-literals-inline", "MyObject");
388388
});
389389

390+
describe("template string", () => {
391+
assertSchema("string-template-literal", "MyObject");
392+
});
393+
390394
describe("custom dates", () => {
391395
assertSchema("custom-dates", "foo.Bar");
392396
});

typescript-json-schema.ts

+38
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,44 @@ export class JsonSchemaGenerator {
724724
if (!!Array.from((<any>propertyType).members)?.find((member: [string]) => member[0] !== "__index")) {
725725
this.getClassDefinition(propertyType, definition);
726726
}
727+
} else if (propertyType.flags & ts.TypeFlags.TemplateLiteral) {
728+
definition.type = "string";
729+
// @ts-ignore
730+
const {texts, types} = propertyType;
731+
const pattern = [];
732+
for (let i = 0; i < texts.length; i++) {
733+
const text = texts[i];
734+
const type = types[i];
735+
736+
if (i === 0) {
737+
pattern.push(`^`);
738+
}
739+
740+
if (type) {
741+
if (type.flags & ts.TypeFlags.String) {
742+
pattern.push(`${text}.*`);
743+
}
744+
745+
if (type.flags & ts.TypeFlags.Number
746+
|| type.flags & ts.TypeFlags.BigInt) {
747+
pattern.push(`${text}[0-9]*`);
748+
}
749+
750+
if (type.flags & ts.TypeFlags.Undefined) {
751+
pattern.push(`${text}undefined`);
752+
}
753+
754+
if (type.flags & ts.TypeFlags.Null) {
755+
pattern.push(`${text}null`);
756+
}
757+
}
758+
759+
760+
if (i === texts.length - 1) {
761+
pattern.push(`${text}$`);
762+
}
763+
}
764+
definition.pattern = pattern.join("");
727765
} else {
728766
definition.type = "array";
729767
if (!definition.items) {

0 commit comments

Comments
 (0)