Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/pattern escape #601

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions test/programs/string-template-literal/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ interface MyObject {
g: `${string}@`,
h: `${number}@`,
i: `${string}@${number}`,
j: `{{${string}}}`
k: `${string}\n`
l: `${string}-${string}`
}
17 changes: 16 additions & 1 deletion test/programs/string-template-literal/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@
"i": {
"type": "string",
"pattern": "^.*@[0-9]*$"
},
"j": {
"pattern": "^\\{\\{.*\\}\\}$",
"type": "string"
},
"k": {
"pattern": "^.*\\n$",
"type": "string"
},
"l": {
"pattern": "^.*-.*$",
"type": "string"
}
},
"additionalProperties": false,
Expand All @@ -51,7 +63,10 @@
"f",
"g",
"h",
"i"
"i",
"j",
"k",
"l"
],
"$schema": "http://json-schema.org/draft-07/schema#"
}
40 changes: 24 additions & 16 deletions typescript-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -751,40 +751,48 @@ export class JsonSchemaGenerator {
definition.type = "string";
// @ts-ignore
const {texts, types} = propertyType;
const pattern = [];
let pattern = "^";
for (let i = 0; i < texts.length; i++) {
const text = texts[i];
const type = types[i];

if (i === 0) {
pattern.push(`^`);
const text: string = texts[i];
const type: ts.Type = types[i];

// https://json-schema.org/understanding-json-schema/reference/regular_expressions
// `-` should not be escaped;
// it is escaped only if it is used like `[a-z]`, which never happens here.
const control = ["[", "]", "(", ")", "{", "}", "^", "$", ".", "|", "?", "*", "+", "!"];
const escaped: { [char: string]: string } = { "\n": "\\n", "\r": "\\r", "\t": "\\t" };
for (const char of text) {
if (control.includes(char)) {
pattern += `\\${char}`;
} else if (escaped[char]) {
pattern += escaped[char];
} else {
pattern += char;
}
}

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

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

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

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


if (i === texts.length - 1) {
pattern.push(`${text}$`);
}
}
definition.pattern = pattern.join("");

pattern += "$";
definition.pattern = pattern;
} else {
definition.type = "array";
if (!definition.items) {
Expand Down
Loading