diff --git a/src/utils/getPattern.spec.ts b/src/utils/getPattern.spec.ts index 09094664..3f3756f0 100644 --- a/src/utils/getPattern.spec.ts +++ b/src/utils/getPattern.spec.ts @@ -10,5 +10,7 @@ describe('getPattern', () => { expect(getPattern('\\')).toEqual('\\\\'); expect(getPattern('\\/')).toEqual('\\\\/'); expect(getPattern('\\/\\/')).toEqual('\\\\/\\\\/'); + // eslint-disable-next-line prettier/prettier + expect(getPattern("'")).toEqual("\\'"); }); }); diff --git a/src/utils/getPattern.ts b/src/utils/getPattern.ts index 58ee7853..b4580f5a 100644 --- a/src/utils/getPattern.ts +++ b/src/utils/getPattern.ts @@ -3,8 +3,12 @@ * However, to use it in HTML or inside new RegExp() we need to * escape the pattern to become: '^\\d{3}-\\d{2}-\\d{4}$' in order * to make it a valid regexp string. + * + * Also, escape single quote characters, because the output uses single quotes for strings + * * @param pattern */ export const getPattern = (pattern?: string): string | undefined => { - return pattern?.replace(/\\/g, '\\\\'); + // eslint-disable-next-line prettier/prettier + return pattern?.replace(/\\/g, '\\\\').replace(/'/g, "\\'"); }; diff --git a/test/__snapshots__/index.spec.ts.snap b/test/__snapshots__/index.spec.ts.snap index f10b9282..71e67103 100644 --- a/test/__snapshots__/index.spec.ts.snap +++ b/test/__snapshots__/index.spec.ts.snap @@ -1299,6 +1299,7 @@ export type ModelWithPattern = { readonly modified?: string; id?: string; text?: string; + patternWithSingleQuotes?: string; }; " @@ -2090,6 +2091,10 @@ export const $ModelWithPattern = { type: 'string', pattern: '^\\\\\\\\w+$', }, + patternWithSingleQuotes: { + type: 'string', + pattern: '^[a-zA-Z0-9\\\\']*$', + }, }, } as const; " @@ -4695,6 +4700,7 @@ export type ModelWithPattern = { readonly modified?: string; id?: string; text?: string; + patternWithSingleQuotes?: string; }; " @@ -5931,6 +5937,10 @@ export const $ModelWithPattern = { type: 'string', pattern: '^\\\\\\\\w+$', }, + patternWithSingleQuotes: { + type: 'string', + pattern: '^[a-zA-Z0-9\\\\']*$', + }, }, } as const; " diff --git a/test/spec/v2.json b/test/spec/v2.json index 2cc00b36..2a45703e 100644 --- a/test/spec/v2.json +++ b/test/spec/v2.json @@ -1499,6 +1499,10 @@ "text": { "type": "string", "pattern": "^\\w+$" + }, + "patternWithSingleQuotes": { + "type": "string", + "pattern": "^[a-zA-Z0-9']*$" } } } diff --git a/test/spec/v3.json b/test/spec/v3.json index aca05aca..e6057f0e 100644 --- a/test/spec/v3.json +++ b/test/spec/v3.json @@ -2466,6 +2466,10 @@ "text": { "type": "string", "pattern": "^\\w+$" + }, + "patternWithSingleQuotes": { + "type": "string", + "pattern": "^[a-zA-Z0-9']*$" } } },