app.param() Error when trying to validate parameters. #1263

Closed
reggi opened this Issue Aug 3, 2012 · 6 comments

Projects

None yet

4 participants

reggi commented Aug 3, 2012

I was reading up in the API Reference under app.param() and I was interested in using it "…to effectively validate parameters, or also parse them to provide capture groups…". When I copy and paste the code example in it logs an error. I'm running express 3.0.0beta7

Code:

    app.param('range', /^(\w+)\.\.(\w+)?$/);

    app.get('/range/:range', function(req, res){
      var range = req.params.range;
      res.send('from ' + range[1] + ' to ' + range[2]);
    });

Error:

/Users/thomas/Desktop/node/platform/node_modules/express/lib/router/index.js:67
    throw new Error('invalid param() call for ' + name + ', got ' + fn);
          ^
Error: invalid param() call for range, got /^(\w+)\.\.(\w+)?$/
    at Router.param (/Users/thomas/Desktop/node/platform/node_modules/express/lib/router/index.js:67:11)
    at app.param (/Users/thomas/Desktop/node/platform/node_modules/express/lib/application.js:232:20)
    at Array.forEach (native)
    at Function.app.param (/Users/thomas/Desktop/node/platform/node_modules/express/lib/application.js:231:9)
    at Object.<anonymous> (/Users/thomas/Desktop/node/platform/app.js:47:5)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
Owner
tj commented Aug 3, 2012

the regexp param thing is in https://github.com/visionmedia/express-params,
they might be worth putting in core but they're certainly more opinionated

juanmac commented Nov 29, 2012

Same error for me trying to capture and validate a mongodb ObjectId which is a 12 bytes string like: 50ae19d2d896e32138000001. This is the pattern i use:

app.param('id', /^[0-9a-z]{24}$/);

And I get: Error: invalid param() call for id, got /^([0-9a-z]){24}$/
I tried to use express-params but it not work. In the readme of express-params says:

"Works with Express 2.5.x".

Could be this the reason why not working in express 3? Thanks!

reggi commented Dec 1, 2012

@jumc It doesn't work like that. It's much more versatile. This should get what you want.

param_regex = function(regex_string){
    return function(req, res, next, id){
        var regex = new RegExp(regex_string);
        if(regex.test(id)){
            next();
        }else{
            next('route');
        }
    }
}

app.param('id', param_regex("));

or just

app.param('id',function(req, res, next, id){
    var regex = new RegExp(/^[0-9a-z]{24}$/);
    if(regex.test(id)){
        next();
    }else{
    next('route');
    }
};
bibig commented Dec 1, 2012

you should add the following part before using app.param('id', /^[0-9a-z]{24}$/);

app.param(function(name, fn){
  if (fn instanceof RegExp) {
    return function(req, res, next, val){
      var captures;
      if (captures = fn.exec(String(val))) {
        req.params[name] = captures;
        next();
      } else {
        next('route');
      }
    }
  }
});
juanmac commented Dec 4, 2012

Sorry guys! I was "living in the past" (express v3.0.0). Thanks for your replies. @bibig I'd already tried using the callback for check for a RegExp but it did not work in v3.0.0 anyway. Thanks again!

Owner
tj commented Dec 6, 2012

closing for now

@tj tj closed this Dec 6, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment