mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 02:31:04 +01:00
respect the iris.WithEmptyFormError option on Context.ReadQuery too
as requested at: #1727
This commit is contained in:
parent
d26b9bfbed
commit
567c06702f
|
@ -28,6 +28,8 @@ The codebase for Dependency Injection, Internationalization and localization and
|
|||
|
||||
## Fixes and Improvements
|
||||
|
||||
- The `iris.WithEmptyFormError` option is respected on `context.ReadQuery` method too, as requested at [#1727](https://github.com/kataras/iris/issues/1727). [Example comments](https://github.com/kataras/iris/blob/master/_examples/request-body/read-query/main.go) were updated.
|
||||
|
||||
- New `httptest.Strict` option setter to enable the `httpexpect.RequireReporter` instead of the default `httpexpect.AssetReporter. Use that to enable complete test failure on the first error. As requested at: [#1722](https://github.com/kataras/iris/issues/1722).
|
||||
|
||||
- New `uuid` builtin path parameter type. Example:
|
||||
|
|
|
@ -17,7 +17,18 @@ func main() {
|
|||
app.Get("/", func(ctx iris.Context) {
|
||||
var t MyType
|
||||
err := ctx.ReadQuery(&t)
|
||||
if err != nil && !iris.IsErrPath(err) {
|
||||
|
||||
// To ignore errors of "required" or when unexpected values are passed to the query,
|
||||
// use the iris.IsErrPath.
|
||||
// It can be ignored, e.g:
|
||||
// if err!=nil && !iris.IsErrPath(err) { ... return }
|
||||
//
|
||||
// To receive an error on EMPTY query when ReadQuery is called
|
||||
// you should enable the `FireEmptyFormError/WithEmptyFormError` ( see below).
|
||||
// To check for the empty error you simple compare the error with the ErrEmptyForm, e.g.:
|
||||
// err == iris.ErrEmptyForm, so, to ignore both path and empty errors, you do:
|
||||
// if err!=nil && err != iris.ErrEmptyForm && !iris.IsErrPath(err) { ctx.StopWithError(...); return }
|
||||
if err != nil {
|
||||
ctx.StopWithError(iris.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
@ -32,5 +43,7 @@ func main() {
|
|||
|
||||
// http://localhost:8080?name=iris&age=3
|
||||
// http://localhost:8080/simple?name=john&name=doe&name=kataras
|
||||
app.Listen(":8080")
|
||||
//
|
||||
// Note: this `WithEmptyFormError` will give an error if the query was empty.
|
||||
app.Listen(":8080", iris.WithEmptyFormError)
|
||||
}
|
||||
|
|
|
@ -497,7 +497,7 @@ var (
|
|||
CookieEncoding = context.CookieEncoding
|
||||
|
||||
// IsErrPath can be used at `context#ReadForm` and `context#ReadQuery`.
|
||||
// It reports whether the incoming error is type of `formbinder.ErrPath`,
|
||||
// It reports whether the incoming error is type of `schema.ErrPath`,
|
||||
// which can be ignored when server allows unknown post values to be sent by the client.
|
||||
//
|
||||
// A shortcut for the `context#IsErrPath`.
|
||||
|
|
|
@ -731,7 +731,7 @@ type Configuration struct {
|
|||
//
|
||||
// See `Context.RecordRequestBody` method for the same feature, per-request.
|
||||
DisableBodyConsumptionOnUnmarshal bool `ini:"disable_body_consumption" json:"disableBodyConsumptionOnUnmarshal,omitempty" yaml:"DisableBodyConsumptionOnUnmarshal" toml:"DisableBodyConsumptionOnUnmarshal"`
|
||||
// FireEmptyFormError returns if set to tue true then the `context.ReadBody/ReadForm`
|
||||
// FireEmptyFormError returns if set to tue true then the `context.ReadForm/ReadQuery/ReadBody`
|
||||
// will return an `iris.ErrEmptyForm` on empty request form data.
|
||||
FireEmptyFormError bool `ini:"fire_empty_form_error" json:"fireEmptyFormError,omitempty" yaml:"FireEmptyFormError" toml:"FireEmptyFormError"`
|
||||
|
||||
|
|
|
@ -2386,8 +2386,11 @@ var (
|
|||
// A shortcut for the `schema#IsErrPath`.
|
||||
IsErrPath = schema.IsErrPath
|
||||
|
||||
// ErrEmptyForm is returned by `context#ReadForm` and `context#ReadBody`
|
||||
// when it should read data from a request form data but there is none.
|
||||
// ErrEmptyForm is returned by
|
||||
// - `context#ReadForm`
|
||||
// - `context#ReadQuery`
|
||||
// - `context#ReadBody`
|
||||
// when the request data (form, query and body respectfully) is empty.
|
||||
ErrEmptyForm = errors.New("empty form")
|
||||
|
||||
// ErrEmptyFormField reports whether a specific field exists but it's empty.
|
||||
|
@ -2460,6 +2463,9 @@ func (ctx *Context) ReadForm(formObject interface{}) error {
|
|||
func (ctx *Context) ReadQuery(ptr interface{}) error {
|
||||
values := ctx.getQuery()
|
||||
if len(values) == 0 {
|
||||
if ctx.app.ConfigurationReadOnly().GetFireEmptyFormError() {
|
||||
return ErrEmptyForm
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@ func Parse(fullpath string, paramTypes []ast.ParamType) ([]*ast.ParamStatement,
|
|||
}
|
||||
// if we have param type path but it's not the last path part
|
||||
if ast.IsTrailing(stmt.Type) && i < len(pathParts)-1 {
|
||||
return nil, fmt.Errorf("%s: parameter type \"%s\" should be registered to the very last of a path", s, stmt.Type.Indent())
|
||||
return nil, fmt.Errorf("%s: parameter type \"%s\" should be registered to the very end of a path", s, stmt.Type.Indent())
|
||||
}
|
||||
|
||||
statements = append(statements, stmt)
|
||||
|
|
Loading…
Reference in New Issue
Block a user