Skip to content

Commit

Permalink
Merge pull request #41 from offirgolan/format-match
Browse files Browse the repository at this point in the history
[BUGFIX] Use String.match instead of RegExp.test to support g flag
  • Loading branch information
offirgolan authored Jan 19, 2017
2 parents 80eed34 + 1b0c17d commit ab5a645
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
3 changes: 2 additions & 1 deletion addon/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
isEmpty,
assert,
deprecate,
canInvoke,
getProperties
} = Ember;

Expand Down Expand Up @@ -68,7 +69,7 @@ export default function validateFormat(value, options, model, attribute) {
set(options, 'regex', regex);
}

if (regex && !regex.test(value)) {
if (!canInvoke(value, 'match') || (regex && isEmpty(value.match(regex)))) {
return validationError(type || 'invalid', value, options);
}

Expand Down
27 changes: 26 additions & 1 deletion tests/unit/validators/format-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ test('email', function(assert) {
'[email protected]'
];
let invalidAddresses = [
null,
undefined,
404,
'plainaddress',
'#@%^%#$@#$@#.com',
'@domain.com',
Expand Down Expand Up @@ -170,17 +173,39 @@ test('url', function(assert) {
});

test('custom', function(assert) {
assert.expect(2);
assert.expect(3);

options = {
regex: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{4,8}$/
};

options = cloneOptions(options);

result = validate(null, options);
assert.equal(processResult(result), 'This field is invalid');

result = validate('password', options);
assert.equal(processResult(result), 'This field is invalid');

result = validate('Pass123', options);
assert.equal(processResult(result), true);
});

test('custom with g flag', function(assert) {
assert.expect(3);

options = {
regex: /foo/g
};

options = cloneOptions(options);

result = validate('foo', options);
assert.equal(processResult(result), true);

result = validate('foo', options);
assert.equal(processResult(result), true);

result = validate('bar', options);
assert.equal(processResult(result), 'This field is invalid');
});

0 comments on commit ab5a645

Please sign in to comment.