Skip to content

Commit 6b37bcd

Browse files
committed
Fixed rule for string where regular expression contains a double quote
1 parent b166f8c commit 6b37bcd

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

lib/rules/string.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ module.exports = function checkString({ schema, messages }, path, context) {
137137

138138
src.push(`
139139
if (!${pattern.toString()}.test(value))
140-
${this.makeError({ type: "stringPattern", expected: "\"" + pattern.toString() + "\"", actual: "origValue", messages })}
140+
${this.makeError({ type: "stringPattern", expected: "\"" + pattern.toString().replace('"', "\\\"") + "\"", actual: "origValue", messages })}
141141
`);
142142
}
143143

test/rules/string.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ describe("Test rule: string", () => {
5050

5151
it("check pattern", () => {
5252
const check = v.compile({ $$root: true, type: "string", pattern: /^[A-Z]+$/ });
53-
53+
console.log(check("John"));
54+
console.log([{ type: "stringPattern", expected: "/^[A-Z]+$/", actual: "John", message: "The '' field fails to match the required pattern." }]);
5455
expect(check("John")).toEqual([{ type: "stringPattern", expected: "/^[A-Z]+$/", actual: "John", message: "The '' field fails to match the required pattern." }]);
5556
expect(check("JOHN")).toEqual(true);
5657
});

test/typescript/rules/string.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,14 @@ describe('TypeScript Definitions', () => {
6464
expect(check('JOHN')).toEqual(true);
6565
});
6666

67-
it('check contains', () => {
67+
it('check pattern with a quote', () => {
68+
const check = v.compile({ $$root: true, type: 'string', pattern: /^[a-z0-9 .\-'?!":;\\/,_]+$/i } as RuleString);
69+
70+
expect(check('John^')).toEqual([{ field: undefined, type: 'stringPattern', expected: '/^[a-z0-9 .\-\'?!":;\\/,_]+$/i', actual: 'John^', message: 'The \'\' field fails to match the required pattern.' }]);
71+
expect(check('JOHN')).toEqual(true);
72+
});
73+
74+
it('check contains', () => {
6875
const check = v.compile({ $$root: true, type: 'string', contains: 'bob' });
6976

7077
expect(check('John')).toEqual([{ type: 'stringContains', expected: 'bob', actual: 'John', message: 'The \'\' field must contain the \'bob\' text.' }]);

0 commit comments

Comments
 (0)