Skip to content

Commit 1756754

Browse files
authored
Merge pull request ferdikoomen#1275 from vanhoofmaarten/fix/pattern-single-quote-escaping
Fix pattern single quote escaping
2 parents d2385a1 + 3927dc6 commit 1756754

File tree

5 files changed

+25
-1
lines changed

5 files changed

+25
-1
lines changed

src/utils/getPattern.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ describe('getPattern', () => {
1010
expect(getPattern('\\')).toEqual('\\\\');
1111
expect(getPattern('\\/')).toEqual('\\\\/');
1212
expect(getPattern('\\/\\/')).toEqual('\\\\/\\\\/');
13+
// eslint-disable-next-line prettier/prettier
14+
expect(getPattern("'")).toEqual("\\'");
1315
});
1416
});

src/utils/getPattern.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
* However, to use it in HTML or inside new RegExp() we need to
44
* escape the pattern to become: '^\\d{3}-\\d{2}-\\d{4}$' in order
55
* to make it a valid regexp string.
6+
*
7+
* Also, escape single quote characters, because the output uses single quotes for strings
8+
*
69
* @param pattern
710
*/
811
export const getPattern = (pattern?: string): string | undefined => {
9-
return pattern?.replace(/\\/g, '\\\\');
12+
// eslint-disable-next-line prettier/prettier
13+
return pattern?.replace(/\\/g, '\\\\').replace(/'/g, "\\'");
1014
};

test/__snapshots__/index.spec.ts.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,7 @@ export type ModelWithPattern = {
12991299
readonly modified?: string;
13001300
id?: string;
13011301
text?: string;
1302+
patternWithSingleQuotes?: string;
13021303
};
13031304

13041305
"
@@ -2090,6 +2091,10 @@ export const $ModelWithPattern = {
20902091
type: 'string',
20912092
pattern: '^\\\\\\\\w+$',
20922093
},
2094+
patternWithSingleQuotes: {
2095+
type: 'string',
2096+
pattern: '^[a-zA-Z0-9\\\\']*$',
2097+
},
20932098
},
20942099
} as const;
20952100
"
@@ -4695,6 +4700,7 @@ export type ModelWithPattern = {
46954700
readonly modified?: string;
46964701
id?: string;
46974702
text?: string;
4703+
patternWithSingleQuotes?: string;
46984704
};
46994705

47004706
"
@@ -5931,6 +5937,10 @@ export const $ModelWithPattern = {
59315937
type: 'string',
59325938
pattern: '^\\\\\\\\w+$',
59335939
},
5940+
patternWithSingleQuotes: {
5941+
type: 'string',
5942+
pattern: '^[a-zA-Z0-9\\\\']*$',
5943+
},
59345944
},
59355945
} as const;
59365946
"

test/spec/v2.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,10 @@
14991499
"text": {
15001500
"type": "string",
15011501
"pattern": "^\\w+$"
1502+
},
1503+
"patternWithSingleQuotes": {
1504+
"type": "string",
1505+
"pattern": "^[a-zA-Z0-9']*$"
15021506
}
15031507
}
15041508
}

test/spec/v3.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2466,6 +2466,10 @@
24662466
"text": {
24672467
"type": "string",
24682468
"pattern": "^\\w+$"
2469+
},
2470+
"patternWithSingleQuotes": {
2471+
"type": "string",
2472+
"pattern": "^[a-zA-Z0-9']*$"
24692473
}
24702474
}
24712475
},

0 commit comments

Comments
 (0)