From d8f124c10d00c7e5dde88c602d966db261aea221 Mon Sep 17 00:00:00 2001 From: Nate Abele Date: Wed, 26 Mar 2014 17:34:25 -0400 Subject: [PATCH] feat($urlMatcherFactory): fail on bad parameters --- src/urlMatcherFactory.js | 8 ++++---- test/urlMatcherFactorySpec.js | 10 ++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/urlMatcherFactory.js b/src/urlMatcherFactory.js index 4dc9db43f..b02622c0b 100644 --- a/src/urlMatcherFactory.js +++ b/src/urlMatcherFactory.js @@ -248,8 +248,8 @@ UrlMatcher.prototype.format = function (values) { param = params[i]; value = values[param]; type = this.params[param]; - // TODO: Maybe we should throw on null here? It's not really good style - // to use '' and null interchangeabley + + if (!type.is(value)) return null; if (value != null) result += encodeURIComponent(type.encode(value)); result += segments[i + 1]; } @@ -270,11 +270,11 @@ function Type(options) { } Type.prototype.is = function(val, key) { - return angular.toJson(this.decode(this.encode(val))) === angular.toJson(val); + return true; }; Type.prototype.encode = function(val, key) { - return String(val); + return val; }; Type.prototype.decode = function(val, key) { diff --git a/test/urlMatcherFactorySpec.js b/test/urlMatcherFactorySpec.js index fdec89661..b5c04efbf 100644 --- a/test/urlMatcherFactorySpec.js +++ b/test/urlMatcherFactorySpec.js @@ -196,5 +196,15 @@ describe("urlMatcherFactory", function () { expect(result.date.toUTCString()).toEqual('Wed, 26 Mar 2014 00:00:00 GMT'); expect(m.format({ date: new Date(2014, 2, 26) })).toBe("/calendar/2014-03-26"); }); + + it("should not match invalid typed parameter values", function () { + var m = new UrlMatcher('/users/{id:int}'); + + expect(m.exec('/users/1138').id).toBe(1138); + expect(m.exec('/users/alpha')).toBeNull(); + + expect(m.format({ id: 1138 })).toBe("/users/1138"); + expect(m.format({ id: "alpha" })).toBeNull(); + }); }); });