From 58afdf088c2c4bfda58e5faab8063239fc3375de Mon Sep 17 00:00:00 2001 From: Lennart Kats Date: Wed, 29 Apr 2015 11:32:32 +0200 Subject: [PATCH] Allow optional parameters to be falsy --- node_modules/frontdoor/lib/route.js | 12 +++-- node_modules/frontdoor/lib/route_test.js | 60 +++++++++++++++++++++++- 2 files changed, 68 insertions(+), 4 deletions(-) diff --git a/node_modules/frontdoor/lib/route.js b/node_modules/frontdoor/lib/route.js index 0c699ab7..d8276197 100644 --- a/node_modules/frontdoor/lib/route.js +++ b/node_modules/frontdoor/lib/route.js @@ -137,6 +137,10 @@ module.exports = function Route(route, options, handler, types) { var key = keys[i]; var param = params[key]; var type = param.type; + if (param.optional && value == null) { + match[key] = value; + continue; + } try { value = type.parse(value); } catch (e) { @@ -157,7 +161,7 @@ module.exports = function Route(route, options, handler, types) { * the decoded and validated parameters are stored in `req.params` * otherwhise an error is returned. */ - function decodeParams(req, res, next) { + var decodeParams = this.decodeParams = function(req, res, next) { var urlParams = req.match; if (!urlParams) return; @@ -197,10 +201,12 @@ module.exports = function Route(route, options, handler, types) { break; value = body[key]; // body is already JSON parsed + if (param.optional && value == null) + break; isValid = type.check(value); break; case "query": - if (param.optional && !(key in query)) + if (param.optional && query[key] == null) break; try { @@ -211,7 +217,7 @@ module.exports = function Route(route, options, handler, types) { isValid = isValid === false ? false : type.check(value); break; case "url": - if (param.optional && !(key in urlParams)) + if (param.optional && urlParams[key] == null) break; value = urlParams[key]; // is already parsed and checked diff --git a/node_modules/frontdoor/lib/route_test.js b/node_modules/frontdoor/lib/route_test.js index 9e11be1c..d1a1bacd 100644 --- a/node_modules/frontdoor/lib/route_test.js +++ b/node_modules/frontdoor/lib/route_test.js @@ -133,7 +133,65 @@ module.exports = { assert.ok(!route.match(req, "/ts/353676299181")); assert.ok(!route.match(req, "/ts/abc")); - } + }, + + "test router: decode parameter in body": function(next) { + var route = new Route("/user", { + params: { + id: { + type: "int", + optional: true, + source: "body" + } + } + }, sinon.stub()); + + var req = { + match: "match", + parsedUrl: { + query: "" + }, + body: { id: 15 } + }; + var res = {}; + + // Note: usually optionals would say 'source: "body",' + // but this should work + route.decodeParams(req, res, function(err, result) { + assert.equal(err, null); + assert.equal(req.params.id, 15); + next(); + }); + }, + + "test router: optional number argument can be falsy": function(next) { + var route = new Route("/user", { + params: { + id: { + type: "int", + optional: true, + source: "body" + } + } + }, sinon.stub()); + + var req = { + match: "match", + parsedUrl: { + query: "" + }, + body: { id: null } + }; + var res = {}; + + // Note: usually optionals would say 'source: "body",' + // but this should work + route.decodeParams(req, res, function(err, result) { + assert.equal(err, null); + assert.equal(req.params.id, null); + next(); + }); + }, }; !module.parent && require("asyncjs").test.testcase(module.exports).exec();