Skip to content

Commit

Permalink
Merge 283e5f3 into 935d4b5
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonslyvia committed Dec 6, 2017
2 parents 935d4b5 + 283e5f3 commit cef2fae
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
22 changes: 22 additions & 0 deletions __tests__/pattern.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,26 @@ describe('pattern', () => {
done();
});
});

it('works for RegExp with global flag', (done) => {
const schema = new Schema({
v: {
pattern: /global/g,
message: 'haha',
},
});

schema.validate({
v: 'globalflag',
}, (errors) => {
expect(errors).toBe(null);
});

schema.validate({
v: 'globalflag',
}, (errors) => {
expect(errors).toBe(null);
done();
});
});
});
4 changes: 4 additions & 0 deletions src/rule/pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import * as util from '../util';
function pattern(rule, value, source, errors, options) {
if (rule.pattern) {
if (rule.pattern instanceof RegExp) {
// if a RegExp instance is passed, reset `lastIndex` in case its `global`
// flag is accidentally set ` to `true`, which in a validation scenario
// is not necessary and the result might be misleading
rule.pattern.lastIndex = 0;
if (!rule.pattern.test(value)) {
errors.push(util.format(options.messages.pattern.mismatch,
rule.fullField, value, rule.pattern));
Expand Down

0 comments on commit cef2fae

Please sign in to comment.