Skip to content

Commit d8f124c

Browse files
committed
feat($urlMatcherFactory): fail on bad parameters
1 parent 8939d05 commit d8f124c

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/urlMatcherFactory.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ UrlMatcher.prototype.format = function (values) {
248248
param = params[i];
249249
value = values[param];
250250
type = this.params[param];
251-
// TODO: Maybe we should throw on null here? It's not really good style
252-
// to use '' and null interchangeabley
251+
252+
if (!type.is(value)) return null;
253253
if (value != null) result += encodeURIComponent(type.encode(value));
254254
result += segments[i + 1];
255255
}
@@ -270,11 +270,11 @@ function Type(options) {
270270
}
271271

272272
Type.prototype.is = function(val, key) {
273-
return angular.toJson(this.decode(this.encode(val))) === angular.toJson(val);
273+
return true;
274274
};
275275

276276
Type.prototype.encode = function(val, key) {
277-
return String(val);
277+
return val;
278278
};
279279

280280
Type.prototype.decode = function(val, key) {

test/urlMatcherFactorySpec.js

+10
Original file line numberDiff line numberDiff line change
@@ -196,5 +196,15 @@ describe("urlMatcherFactory", function () {
196196
expect(result.date.toUTCString()).toEqual('Wed, 26 Mar 2014 00:00:00 GMT');
197197
expect(m.format({ date: new Date(2014, 2, 26) })).toBe("/calendar/2014-03-26");
198198
});
199+
200+
it("should not match invalid typed parameter values", function () {
201+
var m = new UrlMatcher('/users/{id:int}');
202+
203+
expect(m.exec('/users/1138').id).toBe(1138);
204+
expect(m.exec('/users/alpha')).toBeNull();
205+
206+
expect(m.format({ id: 1138 })).toBe("/users/1138");
207+
expect(m.format({ id: "alpha" })).toBeNull();
208+
});
199209
});
200210
});

0 commit comments

Comments
 (0)