Skip to content

Commit fdaedf0

Browse files
committed
Merge branch 'master' of github.com:icebob/fastest-validator
2 parents 6ba613f + d29340f commit fdaedf0

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ describe("Test rule: string", () => {
6262
expect(check("JOHN")).toEqual(true);
6363
});
6464

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

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)