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

Binding for URL Params #846

Closed
miketonks opened this issue Mar 23, 2017 · 28 comments · Fixed by #1694
Closed

Binding for URL Params #846

miketonks opened this issue Mar 23, 2017 · 28 comments · Fixed by #1694
Assignees
Milestone

Comments

@miketonks
Copy link

miketonks commented Mar 23, 2017

Is there a way to validate URL params?

e.g.

r.GET("/:foo/*bar", myFunc)

How can I validate foo and bar?

It would be nice if c.Bind() method could fall back (query params, form params, url params) - maybe introducing a 'url' tag:

type MyParams struct {
    Foo string `url:"foo"`
    Bar string `url:"bar`
    Download bool `form:"dl"`
}
@singggum3b
Copy link

I'm wondering this too ?

@javierprovecho
Copy link
Member

@miketonks that's an interesting feature, however the bind validator would need the fingerprint of the path, or just try to un marshall the c.Params(...) into an struct. need to think about it...

@thinkerou
Copy link
Member

We can get these value using c.Param("foo") and c.Param("bar"), but I don't understand we need to validate what? The type of value which got by c.Param("foo"), right?

@splokhikh
Copy link

Will it be implemented? I'm interest it too

@Szasza
Copy link

Szasza commented Oct 28, 2018

@javierprovecho do you happen to have any updates on this one?

@thinkerou
Copy link
Member

@Szasza can you describe the issue requirement? thanks!

@Szasza
Copy link

Szasza commented Oct 28, 2018

@thinkerou thank you for the ultra-quick response, it is much appreciated.

Use case: having an URL path like /books/:id, where ID is let's say an UUID. I would like to be able to use struct-based binding using tags to validate the URL path parameter id.

Problem: Currently the above is not possible in a straightforward way, because:

  • the bind and shouldBind functions only exist for query parameters, and request bodies, but not for URL path parameter
  • using the context there is no way to mutate the request query or body, which would enable to put the URL params into the request query/body and use the existing binding functions

One workaround I can think of from the top of my head is creating a new http.Request, copying the URL path parameter names and values into the new request's query parameters, and running bindWith using that, but it is extra code in each handler which could be simplified.

I hope the above helps.

@thinkerou
Copy link
Member

thinkerou commented Oct 28, 2018

wip demo: #1612

or just try to un marshall the c.Params(...) into an struct

Not un marshall it into struct, but traverse it and mapForm its element.

@Szasza
Copy link

Szasza commented Oct 29, 2018

@thinkerou thank you for the quick WIP demo, it works perfectly fine with valid values.

I am unsure if the error handling is considered as finalised in the changeset, but if I set uuid as the binding's value, and send an invalid uuid as the path parameter value, it yields an unhandled panic.

Thank you again.

@thinkerou
Copy link
Member

@Szasza you are welcome!
uuid likes xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, and uri is /rest/n/:uuid, now visit /rest/n/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx will trigger binding, but how should check it? because input uuid is a string. thanks!

@Szasza
Copy link

Szasza commented Oct 29, 2018

@thinkerou I think there is a misunderstanding here, please allow me to clarify it.

When I bind let's say the query parameters of a request, there are two tags that I use for the request struct's fields:

  • form:"id" - it binds the id query param to the struct field
  • binding:"uuid" - this is a pass-through to validator.v8, the value is validated against the uuid validation rule.

Is it possible to have the same pass-through for the binding tag in case of an uri:"id" binding as well and return a proper validation message instead of a panic? This would be useful since the router does not support regexp-based URI parameter matching.

I hope the above helps, please let me know if further clarification is needed, and thank you again.

@thinkerou
Copy link
Member

@Szasza thanks!
Now #1612 have supported bind uri params, please help me review, thanks everybody!

@Szasza
Copy link

Szasza commented Nov 4, 2018

@thinkerou my pleasure, thank you for the quick implementation, it is much appreciated 😄

@axiaoxin
Copy link
Contributor

axiaoxin commented Nov 16, 2018

Why not support the type of params specified in URL? like

router.GET("/post/<int:post_id>", Handler)

@thinkerou
Copy link
Member

@axiaoxin IMO, that is too complex.

@miketonks
Copy link
Author

Thanks for adding this feature, it looks great.

If anyone is interested we solved the problem another way, which can be used on top of / alongside gin validation.

See: https://github.com/miketonks/swag-validator

@thinkerou thinkerou added this to the 1.4 milestone Nov 22, 2018
@thinkerou
Copy link
Member

#1612 merged!

@dszakallas
Copy link

dszakallas commented Dec 2, 2018

Thanks! This is great. Why not add BindUri and MustBindUri too?

@halink0803
Copy link

When there will be a new release? This feature is useful and I don't want to fork to the latest commit.

@thinkerou
Copy link
Member

thinkerou commented Dec 12, 2018

Thanks! This is great. Why not add BindUri and MustBindUri too?

@dszakallas I will add it. thanks!

When there will be a new release? This feature is useful and I don't want to fork to the latest commit.

@halink0803 1.4 version on February 28, 2019, and publish release need to @javierprovecho help.

@thinkerou thinkerou reopened this Dec 12, 2018
@thinkerou thinkerou mentioned this issue Dec 12, 2018
@thinkerou
Copy link
Member

@dszakallas #1694 add BindUri.

@halink0803
Copy link

@thinkerou Thank you for quick response, I think we will fork latest commit by then.

@thinkerou thinkerou self-assigned this Dec 12, 2018
@cappuccino5
Copy link

cappuccino5 commented Apr 30, 2019

please there problem deal? I too like use it

@thinkerou
Copy link
Member

@cappuccino5 what's mean?

@SilkageNet
Copy link

please how bind uri to map

@zhujinhe
Copy link

Btw, should Uri be all uppercase?

@asbjornu
Copy link

asbjornu commented Aug 3, 2022

If I want a model to be bound to both form and uri parameters, do I need to call several Bind methods? Calling ShouldBind() doesn't seem to bind uri parameters.

@hrQAQ
Copy link

hrQAQ commented Oct 4, 2022

@asbjornu Maybe you need to try ShouldBindWith()

this doc has some more info !

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

Successfully merging a pull request may close this issue.