Skip to content
Myr

Added checking of app.param() fn retval

Before adoption: this commit came in with the history the project adopted, so no landing records it.

functions returning NaN, == null, or === false are considered a failure and the next route is executed

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent c24b2fa · Browse files · 2 files+49 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+49 added−2 removed

  • tests:+35 added−0 removed
  • other:+14 added−2 removed

2 files+49 added−2 removed

modified:lib/router/index.js+14 added-2 removed
95 unmodified lines
9697 (function pass(i){98 if (route = match(req, routes, i)) {99 var i = 0100 , keys = route.keys;100 , keys = route.keys101 , ret;102103 req.params = route.params;104105 // Param preconditions
11 unmodified lines
117 // Param has callback118 } else if (fn) {119 // Return style120 if (1 == fn.length) {120 req.params[key] = fn(val);121 ret = req.params[key] = fn(val);122 if (invalidParam(ret)) {123 pass(req._route_index + 1);124 return;125 }126 param();127 // Middleware style128 } else {129 fn(req, res, param, val);
203 unmodified lines
333 }334 }335 }336}337338function invalidParam(val) {339 return null == val340 || false === val341 || ('number' == typeof val && isNaN(val))342 ;343}
modified:test/router.test.js+35 added
150 unmodified lines
151 { url: '/users/0-3' },152 { body: 'users tj, tobi, loki' });153 },154 155 'test .param() ret val': function(){156 var app = express.createServer();157158 app.param('uid', function(id){ return parseInt(id) });159160 app.get('/user/:uid', function(req, res, next){161 var id = req.params.uid;162 res.send('loaded user ' + id + ' as typeof ' + typeof id);163 });164165 app.param('name', function(name){166 var captures;167 if (captures = /^\w[\w\d]+/.exec(name)) {168 return captures[0];169 }170 });171172 app.get('/user/:name', function(req, res, next){173 var name = req.params.name;174 res.send('loaded user ' + name);175 });176177 assert.response(app,178 { url: '/user/0' },179 { body: 'loaded user 0 as typeof number' });180181 assert.response(app,182 { url: '/user/tj' },183 { body: 'loaded user tj' });184185 assert.response(app,186 { url: '/user/t' },187 { status: 404 });188 },189 190 'test OPTIONS': function(){191 var app = express.createServer();192 193 app.get('/', function(){});
125 unmodified lines