Skip to content

Commit 3927dc6

Browse files
committed
Fix pattern single quote escaping
1 parent abab307 commit 3927dc6

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
@@ -1298,6 +1298,7 @@ export type ModelWithPattern = {
12981298
readonly modified?: string;
12991299
id?: string;
13001300
text?: string;
1301+
patternWithSingleQuotes?: string;
13011302
};
13021303

13031304
"
@@ -2089,6 +2090,10 @@ export const $ModelWithPattern = {
20892090
type: 'string',
20902091
pattern: '^\\\\\\\\w+$',
20912092
},
2093+
patternWithSingleQuotes: {
2094+
type: 'string',
2095+
pattern: '^[a-zA-Z0-9\\\\']*$',
2096+
},
20922097
},
20932098
} as const;
20942099
"
@@ -4693,6 +4698,7 @@ export type ModelWithPattern = {
46934698
readonly modified?: string;
46944699
id?: string;
46954700
text?: string;
4701+
patternWithSingleQuotes?: string;
46964702
};
46974703

46984704
"
@@ -5929,6 +5935,10 @@ export const $ModelWithPattern = {
59295935
type: 'string',
59305936
pattern: '^\\\\\\\\w+$',
59315937
},
5938+
patternWithSingleQuotes: {
5939+
type: 'string',
5940+
pattern: '^[a-zA-Z0-9\\\\']*$',
5941+
},
59325942
},
59335943
} as const;
59345944
"

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)