Fix pattern single quote escaping

This commit is contained in:
Maarten Van Hoof 2022-10-03 13:11:48 +02:00
parent abab30743b
commit 3927dc6220
5 changed files with 25 additions and 1 deletions

View File

@ -10,5 +10,7 @@ describe('getPattern', () => {
expect(getPattern('\\')).toEqual('\\\\');
expect(getPattern('\\/')).toEqual('\\\\/');
expect(getPattern('\\/\\/')).toEqual('\\\\/\\\\/');
// eslint-disable-next-line prettier/prettier
expect(getPattern("'")).toEqual("\\'");
});
});

View File

@ -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, "\\'");
};

View File

@ -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;
"

View File

@ -1499,6 +1499,10 @@
"text": {
"type": "string",
"pattern": "^\\w+$"
},
"patternWithSingleQuotes": {
"type": "string",
"pattern": "^[a-zA-Z0-9']*$"
}
}
}

View File

@ -2466,6 +2466,10 @@
"text": {
"type": "string",
"pattern": "^\\w+$"
},
"patternWithSingleQuotes": {
"type": "string",
"pattern": "^[a-zA-Z0-9']*$"
}
}
},