iris icon indicating copy to clipboard operation
iris copied to clipboard

[QUESTION] Read* like ReadQuery ignores "required" tags and don't kick out an error

Open Dexus opened this issue 3 years ago • 11 comments

Describe the bug Schema supportet Reader dont respect "required" tag

To Reproduce https://github.com/kataras/iris/blob/master/_examples/request-body/read-query/main.go < don't send a query with name in it -> no error

Expected behavior Requests without the required fileds, should return an error.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: Ubuntu 18.04

iris.Version

  • master

Additional context Maybe the https://github.com/iris-contrib/schema need an update and/or fixes?

Dexus avatar Feb 14 '21 10:02 Dexus

Okay i found out that if you don't provide any querystrings you don't get a error. But if you only provide only one you get the errors.

I think this needs to be fixed.

type MyQuery struct {
	Session  string `url:"s,required"`
	Page     string `url:"p,required"`
	SubPage  string `url:"sp"`
	Action   string `url:"a"`
}

https://mydomain.org/pagewithqueryneed -> no error https://mydomain.org/pagewithqueryneed?notneed -> error => s is empty (and 1 other error) https://mydomain.org/pagewithqueryneed?p -> error = s is empty

Dexus avatar Feb 14 '21 10:02 Dexus

To Reproduce https://github.com/kataras/iris/blob/master/_examples/request-body/read-query/main.go < don't send a query with name in it -> no error

@Dexus this is working on me.

image

image

And your example also works:

(p is missing): image

(s is missing): image

(both are missing):

image

Maybe you have the iris.IsErrPath(err) ignored?

kataras avatar Feb 14 '21 11:02 kataras

Try it without a querystring attached and then there is no error

Von meinem iPhone gesendet

Am 14.02.2021 um 12:20 schrieb Gerasimos (Makis) Maropoulos [email protected]:

 To Reproduce https://github.com/kataras/iris/blob/master/_examples/request-body/read-query/main.go < don't send a query with name in it -> no error

@Dexus this is working on me.

Maybe you have the iris.IsErrPath(err) ignored?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

Dexus avatar Feb 14 '21 11:02 Dexus

Of course there is no error without query string. You have to first check if the URL Query is empty. This is intentional:

func (ctx *Context) ReadQuery(ptr interface{}) error {
	values := ctx.getQuery()
	if len(values) == 0 {
		return nil // HERE
	}
	
	// ...

But if you want we can respect the FireEmptyFormError configuration for queries as we do on ReadForm:

func (ctx *Context) ReadForm(formObject interface{}) error {
	values := ctx.FormValues()
	if len(values) == 0 {
		if ctx.app.ConfigurationReadOnly().GetFireEmptyFormError() {
			return ErrEmptyForm
		}
		return nil
	}
	
	// ....

kataras avatar Feb 14 '21 11:02 kataras

That would be a greate idea!

Dexus avatar Feb 14 '21 11:02 Dexus

OK I am on it! Can you also please reply to you other issues labeld as "BUG" too? (e.g. https://github.com/kataras/iris/issues/1723) thanks!

kataras avatar Feb 14 '21 11:02 kataras

OK It's done, upgrade on latest @master. The example was also updated to have comments about this feature.

kataras avatar Feb 14 '21 11:02 kataras

@kataras did update the other issue

Dexus avatar Feb 14 '21 11:02 Dexus

@kataras

it would be cool if we can alias schema.MultiError to catch the error of this type.

Because schema will collect the errors into this type.

Dexus avatar Feb 14 '21 16:02 Dexus

@Dexus Hmm what do you mean? If the data are empty, there is no other "error" that can happen (so MultiError has no reason to contain that error), decoder won't be fired at all to check the fields (the source is empty). Can you give me a usecase example so I provide a better option for you?

kataras avatar Feb 14 '21 21:02 kataras

Hi @kataras

if I have only one required field filled, i get an error of MultiError (e#10003):


var (
	errMultiError = &schema.MultiError{}
	errEmptyField = &schema.EmptyFieldError{}
)

// CheckErr ...
func CheckErr(ctx iris.Context, err error) bool {

	if err != nil && err == iris.ErrEmptyForm {
		ctx.StopWithError(iris.StatusBadRequest, errors.New("error (e#10001)"))
		ctx.StopExecution()
		return true
	} else if err != nil && iris.IsErrPath(err) {
		ctx.StopWithError(iris.StatusBadRequest, errors.New("error (e#10002)"))
		ctx.StopExecution()
		return true
	} else if err != nil && errors.Is(err, iris.ErrEmptyFormField) {
		ctx.StopWithError(iris.StatusBadRequest, errors.New("error (e#10005)"))
		ctx.StopExecution()
		return true
	} else if err != nil && errors.As(err, errMultiError) {
		ctx.StopWithError(iris.StatusBadRequest, errors.New("error (e#10003)"))
		ctx.StopExecution()
		return true
	} else if err != nil && errors.As(err, errEmptyField) {
		ctx.StopWithError(iris.StatusBadRequest, errors.New("error (e#10004)"))
		ctx.StopExecution()
		return true
	} else if err != nil {
		ctx.StopWithError(iris.StatusInternalServerError, err)
		ctx.StopExecution()
		return true
	}
	return false
}

Dexus avatar Feb 15 '21 10:02 Dexus