Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Named predefined params #23

Closed
rodrigok opened this issue May 23, 2014 · 4 comments
Closed

Named predefined params #23

rodrigok opened this issue May 23, 2014 · 4 comments

Comments

@rodrigok
Copy link

Original issue: expressjs/express#2127

Using this code from doc

app.param (name, regexp) ->
    if Object.prototype.toString.call(regexp) == '[object RegExp]'
        return (req, res, next, val) ->
            if captures = regexp.exec(String(val))
                req.params[name] = captures[0]
                next()
            else
                next('route')

I can use named params with regex match, but I can't use this param more than 1 time and generic names make the url a little messy.

I can't do this

app.get '/purchase/:objectId/:objectId', (req, res, next) ->

There are some way to name this params like below?

app.get '/purchase/:(objectId)place/:(objectId)user', (req, res, next) ->
    console.log req.params.place, req.params.user
@blakeembrey
Copy link
Member

@rodrigok Do you have more information on exactly what you're trying to achieve? From your example, I don't know why you couldn't just do /purchase/:place/:user.

@rodrigok
Copy link
Author

Yes, sorry.

I'm trying to validate params to only accept hexadecimal characters using a predefined "type" called objectId.

Exists an example at express website (http://expressjs.com/4x/api.html#app.param) that allow pass regexp to validate params by param's name.

My proposal is find some way to allow to validate params via an type and not via param's name, some thing like this.

It's just an idea and will help-me a lot.

Thanks.

@blakeembrey
Copy link
Member

Ok, I think I understand now. You're after the ability to do aliases for the purpose of param validation. There are a couple of reasons I think this is a bad idea.

  1. Each parameter should be distinct values, otherwise things other than validation (like loading a user) will break.
  2. You can easily reuse validation over multiple parameters by assigning the regex to a variable (or better, making a utility file and requiring it) and reusing it. This will allow you to do the objectId validation as much as you want and also push logic onto the stack after it to load the place/user/etc.

Feel free to reopen if you disagree or comment if it still doesn't make a ton of sense.

@rodrigok
Copy link
Author

Ok, I understand your points.

Thanks for your attention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants