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

[BUG] Validator do not fire even if required #2487

Closed
Dexus opened this issue Sep 5, 2024 · 2 comments
Closed

[BUG] Validator do not fire even if required #2487

Dexus opened this issue Sep 5, 2024 · 2 comments
Assignees

Comments

@Dexus
Copy link

Dexus commented Sep 5, 2024

Describe the bug
The Validator dont fire on ctx.ReadQuery(...)
I was doing it similar to

// `ReadJSON`, `ReadXML`, `ReadMsgPack`, `ReadYAML`, `ReadForm`, `ReadQuery`, `ReadBody`.

To Reproduce

type TypeFromTo struct {
	StartDate string `json:"startDate" url:"startDate" param:"startDate" validate:"required,iso_8601_date"`
	EndDate   string `json:"endDate" url:"endDate" param:"endDate" validate:"required,iso_8601_date"`
}

v1.Get("/stats", func(ctx iris.Context) {
	var t TypeFromTo
	err := ctx.ReadQuery(&t)
	// 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 {
		// Handle the error, below you will find the right way to do that...

		if errs, ok := err.(validator.ValidationErrors); ok {
			// Wrap the errors with JSON format, the underline library returns the errors as interface.
			validationErrors := wrapValidationErrors(errs)

			// Fire an application/json+problem response and stop the handlers chain.
			ctx.StopWithProblem(iris.StatusBadRequest, iris.NewProblem().
				Title("Validation error").
				Detail("One or more fields failed to be validated").
				Type("/user/validation-errors").
				Key("errors", validationErrors))

			return
		}

		// It's probably an internal JSON error, let's dont give more info here.
		ctx.StopWithStatus(iris.StatusInternalServerError)
		return
  	}
	
	ctx.Json(iris.Map{
		"demo": true,
	})
}

call the url /stats without ?startDate=2024-01-01&endDate=2024-02-01

Expected behavior
If no query parms are there with the names it shoulf fire the errors

Screenshots

Desktop (please complete the following information):

  • OS: windows

iris.Version

  • main

Additional context

@Dexus
Copy link
Author

Dexus commented Sep 5, 2024

Hi,

current fix for me is:

var t TypeFromTo
err := ctx.ReadQuery(&t)
if err == nil {
	err = ctx.Application().Validate(t)
}

@Dexus
Copy link
Author

Dexus commented Sep 6, 2024

ok, forget what I was reporting, forget to set the iris.With....

@Dexus Dexus closed this as completed Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants