kataras / iris

The fastest HTTP/2 Go Web Framework. New, modern and easy to learn. Fast development with Code you control. Unbeatable cost-performance ratio :rocket:
https://www.iris-go.com
BSD 3-Clause "New" or "Revised" License
25.23k stars 2.47k forks source link

How to make `context.ReadQuery()` ignore undefined filed #1533

Closed axetroy closed 4 years ago

axetroy commented 4 years ago

Is your feature request related to a problem? Please describe.

The query in url should be variable

If the client passes in an undefined field, then context.ReadQuery() should not throw an error

Describe the solution you'd like

ignore undefined field

type Query struct {
  Limit int `url: "limit"`
}

var query Query

// pass `true` for ignore undefined field
context.ReadQuery(&query, true)

// pass the path `/example?limit=10&undefined_field=123`

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context Add any other context or screenshots about the feature request here.

kataras commented 4 years ago

Hello @axetroy,

Iris returns a special error on unknown incoming fields, you can use the iris.IsErrPath to ignore that error on read query/form. Simply add an !iris.IsErrPath(err) in your if statement on the error returned from ctx.ReadQuery and you are ready to go.

Example: https://github.com/kataras/iris/blob/3fae2c31c613dca7fa355642d4db5d24e208407f/_examples/request-body/read-query/main.go#L17-L22

kataras commented 4 years ago

There are examples for that, but the ReadQuery method missed a comment documentation. I've just added it there too.

If you are OK with the solution provided above, close the issue, if not, please post an idea for an alternative solution that will not require a breaking change of the ReadQuery usage.

Thanks, Gerasimos Maropoulos

axetroy commented 4 years ago

This is a solution for me, thanks @kataras