From 810bdc49a4eb3e21544261af4248d09a38b02746 Mon Sep 17 00:00:00 2001 From: can Date: Mon, 28 Aug 2023 17:36:43 +0800 Subject: [PATCH 1/2] fix https://github.com/YousefED/typescript-json-schema/issues/426 --- test/programs/string-template-literal/main.ts | 10 ++++ .../string-template-literal/schema.json | 52 +++++++++++++++++++ test/schema.test.ts | 4 ++ typescript-json-schema.ts | 36 +++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 test/programs/string-template-literal/main.ts create mode 100644 test/programs/string-template-literal/schema.json diff --git a/test/programs/string-template-literal/main.ts b/test/programs/string-template-literal/main.ts new file mode 100644 index 00000000..617ced83 --- /dev/null +++ b/test/programs/string-template-literal/main.ts @@ -0,0 +1,10 @@ +interface MyObject { + a: `@${string}`, + b: `@${number}`, + c: `@${bigint}`, + d: `@${boolean}`, + e: `@${undefined}`, + f: `@${null}`, + g: `${string}@`, + h: `${string}@${number}`, +} \ No newline at end of file diff --git a/test/programs/string-template-literal/schema.json b/test/programs/string-template-literal/schema.json new file mode 100644 index 00000000..f0c87f02 --- /dev/null +++ b/test/programs/string-template-literal/schema.json @@ -0,0 +1,52 @@ +{ + "type": "object", + "properties": { + "a": { + "type": "string", + "pattern": "^@" + }, + "b": { + "type": "string", + "pattern": "^@" + }, + "c": { + "type": "string", + "pattern": "^@" + }, + "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]*" + } + }, + "additionalProperties": false, + "required": [ + "a", + "b", + "c", + "d", + "e", + "f", + "g", + "h" + ], + "$schema": "http://json-schema.org/draft-07/schema#" + } \ No newline at end of file diff --git a/test/schema.test.ts b/test/schema.test.ts index dc1f4e86..14069c50 100644 --- a/test/schema.test.ts +++ b/test/schema.test.ts @@ -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"); }); diff --git a/typescript-json-schema.ts b/typescript-json-schema.ts index 8d1212b8..becd5e47 100644 --- a/typescript-json-schema.ts +++ b/typescript-json-schema.ts @@ -724,6 +724,42 @@ export class JsonSchemaGenerator { if (!!Array.from((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]; + if (i === 0) { + pattern.push(text === "" ? text : `^${text}`); + } + + if (i === texts.length - 1) { + pattern.push(text === "" ? text : `${text}$`); + } + + const type = types[i]; + if (i >= 1 && 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`); + } + } + } + definition.pattern = pattern.join(""); } else { definition.type = "array"; if (!definition.items) { From 2c6bbf708abedf93a5054e1d426015240274f1a0 Mon Sep 17 00:00:00 2001 From: can Date: Mon, 28 Aug 2023 18:22:13 +0800 Subject: [PATCH 2/2] fix https://github.com/YousefED/typescript-json-schema/issues/426 --- test/programs/string-template-literal/main.ts | 3 ++- .../string-template-literal/schema.json | 19 ++++++++++++------- typescript-json-schema.ts | 16 +++++++++------- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/test/programs/string-template-literal/main.ts b/test/programs/string-template-literal/main.ts index 617ced83..4b0745b0 100644 --- a/test/programs/string-template-literal/main.ts +++ b/test/programs/string-template-literal/main.ts @@ -6,5 +6,6 @@ interface MyObject { e: `@${undefined}`, f: `@${null}`, g: `${string}@`, - h: `${string}@${number}`, + h: `${number}@`, + i: `${string}@${number}`, } \ No newline at end of file diff --git a/test/programs/string-template-literal/schema.json b/test/programs/string-template-literal/schema.json index f0c87f02..cc39dd2c 100644 --- a/test/programs/string-template-literal/schema.json +++ b/test/programs/string-template-literal/schema.json @@ -3,15 +3,15 @@ "properties": { "a": { "type": "string", - "pattern": "^@" + "pattern": "^@.*$" }, "b": { "type": "string", - "pattern": "^@" + "pattern": "^@[0-9]*$" }, "c": { "type": "string", - "pattern": "^@" + "pattern": "^@[0-9]*$" }, "d": { "enum": [ @@ -30,11 +30,15 @@ }, "g": { "type": "string", - "pattern": "@$" + "pattern": "^.*@$" }, "h": { "type": "string", - "pattern": "@[0-9]*" + "pattern": "^[0-9]*@$" + }, + "i": { + "type": "string", + "pattern": "^.*@[0-9]*$" } }, "additionalProperties": false, @@ -46,7 +50,8 @@ "e", "f", "g", - "h" + "h", + "i" ], "$schema": "http://json-schema.org/draft-07/schema#" - } \ No newline at end of file +} \ No newline at end of file diff --git a/typescript-json-schema.ts b/typescript-json-schema.ts index becd5e47..2337b64c 100644 --- a/typescript-json-schema.ts +++ b/typescript-json-schema.ts @@ -731,16 +731,13 @@ export class JsonSchemaGenerator { const pattern = []; for (let i = 0; i < texts.length; i++) { const text = texts[i]; - if (i === 0) { - pattern.push(text === "" ? text : `^${text}`); - } + const type = types[i]; - if (i === texts.length - 1) { - pattern.push(text === "" ? text : `${text}$`); + if (i === 0) { + pattern.push(`^`); } - const type = types[i]; - if (i >= 1 && type) { + if (type) { if (type.flags & ts.TypeFlags.String) { pattern.push(`${text}.*`); } @@ -758,6 +755,11 @@ export class JsonSchemaGenerator { pattern.push(`${text}null`); } } + + + if (i === texts.length - 1) { + pattern.push(`${text}$`); + } } definition.pattern = pattern.join(""); } else {