Skip to content

Commit 32b27db

Browse files
committed
feat(UrlMatcher): validates whole interface
urlMatcherFactoryProvider.isMatcher() now validates the entire UrlMatcher interface.
1 parent a3e2136 commit 32b27db

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/urlMatcherFactory.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -449,13 +449,22 @@ function $UrlMatcherFactory() {
449449
* @methodOf ui.router.util.$urlMatcherFactory
450450
*
451451
* @description
452-
* Returns true if the specified object is a UrlMatcher, or false otherwise.
452+
* Returns true if the specified object is a `UrlMatcher`, or false otherwise.
453453
*
454454
* @param {Object} object The object to perform the type check against.
455-
* @returns {Boolean} Returns `true` if the object has the following functions: `exec`, `format`, and `concat`.
455+
* @returns {Boolean} Returns `true` if the object matches the `UrlMatcher` interface, by
456+
* implementing all the same methods.
456457
*/
457458
this.isMatcher = function (o) {
458-
return isObject(o) && isFunction(o.exec) && isFunction(o.format) && isFunction(o.concat);
459+
if (!isObject(o)) return false;
460+
var result = true;
461+
462+
forEach(UrlMatcher.prototype, function(val, name) {
463+
if (isFunction(val)) {
464+
result = result && (isDefined(o[name]) && isFunction(o[name]));
465+
}
466+
});
467+
return result;
459468
};
460469

461470
this.type = function (name, def) {

test/urlMatcherFactorySpec.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ describe("UrlMatcher", function () {
2929
provider.strictMode(false);
3030
expect(provider.compile('/hello').exec('/hello/')).toEqual({});
3131
});
32+
33+
it("should correctly validate UrlMatcher interface", function () {
34+
var m = new UrlMatcher("/");
35+
expect(provider.isMatcher(m)).toBe(true);
36+
37+
m.validates = null;
38+
expect(provider.isMatcher(m)).toBe(false);
39+
});
3240
});
3341

3442
it("should match static URLs", function () {
@@ -177,7 +185,13 @@ describe("urlMatcherFactory", function () {
177185
it("recognizes matchers", function () {
178186
expect($umf.isMatcher(new UrlMatcher('/'))).toBe(true);
179187

180-
var custom = { format: angular.noop, exec: angular.noop, concat: angular.noop };
188+
var custom = {
189+
format: angular.noop,
190+
exec: angular.noop,
191+
concat: angular.noop,
192+
validates: angular.noop,
193+
parameters: angular.noop
194+
};
181195
expect($umf.isMatcher(custom)).toBe(true);
182196
});
183197

0 commit comments

Comments
 (0)