Skip to content

Commit

Permalink
feat($urlMatcherFactory): fail on bad parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
nateabele committed Apr 11, 2014
1 parent 8939d05 commit d8f124c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand All @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions test/urlMatcherFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

0 comments on commit d8f124c

Please sign in to comment.