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 · 25 comments · Fixed by #1694

Comments

@miketonks
Copy link

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

This comment has been minimized.

Copy link

commented Jul 7, 2017

I'm wondering this too ?

@javierprovecho

This comment has been minimized.

Copy link
Member

commented Jul 8, 2017

@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

This comment has been minimized.

Copy link
Member

commented Nov 20, 2017

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?

@plokhikh

This comment has been minimized.

Copy link

commented Oct 22, 2018

Will it be implemented? I'm interest it too

@Szasza

This comment has been minimized.

Copy link

commented Oct 28, 2018

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

@thinkerou

This comment has been minimized.

Copy link
Member

commented Oct 28, 2018

@Szasza can you describe the issue requirement? thanks!

@Szasza

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Member

commented Oct 29, 2018

@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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Member

commented Nov 3, 2018

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

@Szasza

This comment has been minimized.

Copy link

commented Nov 4, 2018

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

@axiaoxin

This comment has been minimized.

Copy link

commented Nov 16, 2018

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

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

This comment has been minimized.

Copy link
Member

commented Nov 20, 2018

@axiaoxin IMO, that is too complex.

@miketonks

This comment has been minimized.

Copy link
Author

commented Nov 20, 2018

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

This comment has been minimized.

Copy link
Member

commented Nov 22, 2018

#1612 merged!

@thinkerou thinkerou closed this Nov 22, 2018
@dszakallas

This comment has been minimized.

Copy link

commented Dec 2, 2018

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

@halink0803

This comment has been minimized.

Copy link

commented Dec 12, 2018

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

@thinkerou

This comment has been minimized.

Copy link
Member

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 referenced this issue Dec 12, 2018
@thinkerou

This comment has been minimized.

Copy link
Member

commented Dec 12, 2018

@dszakallas #1694 add BindUri.

@halink0803

This comment has been minimized.

Copy link

commented Dec 12, 2018

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

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

This comment has been minimized.

Copy link

commented Apr 30, 2019

please there problem deal? I too like use it

@thinkerou

This comment has been minimized.

Copy link
Member

commented May 5, 2019

@cappuccino5 what's mean?

@SilkageNet

This comment has been minimized.

Copy link

commented Jul 10, 2019

please how bind uri to map

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
You can’t perform that action at this time.