diff --git a/addon/format.js b/addon/format.js index b51d973..d4eadb9 100755 --- a/addon/format.js +++ b/addon/format.js @@ -13,6 +13,7 @@ const { isEmpty, assert, deprecate, + canInvoke, getProperties } = Ember; @@ -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); } diff --git a/tests/unit/validators/format-test.js b/tests/unit/validators/format-test.js index c98bdcc..0f951e5 100644 --- a/tests/unit/validators/format-test.js +++ b/tests/unit/validators/format-test.js @@ -60,6 +60,9 @@ test('email', function(assert) { 'EMAIL@DOMAIN.COM' ]; let invalidAddresses = [ + null, + undefined, + 404, 'plainaddress', '#@%^%#$@#$@#.com', '@domain.com', @@ -170,7 +173,7 @@ test('url', function(assert) { }); test('custom', function(assert) { - assert.expect(2); + assert.expect(3); options = { regex: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{4,8}$/ @@ -178,9 +181,31 @@ test('custom', function(assert) { 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'); +});