From 3927dc6220252e9b04cc6871d59691fc8fea8efe Mon Sep 17 00:00:00 2001 From: Maarten Van Hoof Date: Mon, 3 Oct 2022 13:11:48 +0200 Subject: [PATCH] Fix pattern single quote escaping --- src/utils/getPattern.spec.ts | 2 ++ src/utils/getPattern.ts | 6 +++++- test/__snapshots__/index.spec.ts.snap | 10 ++++++++++ test/spec/v2.json | 4 ++++ test/spec/v3.json | 4 ++++ 5 files changed, 25 insertions(+), 1 deletion(-) 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 cb3405f2..c47d9c66 100644 --- a/test/__snapshots__/index.spec.ts.snap +++ b/test/__snapshots__/index.spec.ts.snap @@ -1298,6 +1298,7 @@ export type ModelWithPattern = { readonly modified?: string; id?: string; text?: string; + patternWithSingleQuotes?: string; }; " @@ -2089,6 +2090,10 @@ export const $ModelWithPattern = { type: 'string', pattern: '^\\\\\\\\w+$', }, + patternWithSingleQuotes: { + type: 'string', + pattern: '^[a-zA-Z0-9\\\\']*$', + }, }, } as const; " @@ -4693,6 +4698,7 @@ export type ModelWithPattern = { readonly modified?: string; id?: string; text?: string; + patternWithSingleQuotes?: string; }; " @@ -5929,6 +5935,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']*$" } } },