Skip to content

Commit

Permalink
escape characters on convertiny template literal
Browse files Browse the repository at this point in the history
  • Loading branch information
cm-ayf committed May 11, 2024
1 parent 3426762 commit 31fb449
Showing 1 changed file with 24 additions and 16 deletions.
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

0 comments on commit 31fb449

Please sign in to comment.