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.29k stars 2.47k forks source link

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

Closed Dexus closed 2 months ago

Dexus commented 2 months ago

Describe the bug The Validator dont fire on ctx.ReadQuery(...) I was doing it similar to https://github.com/kataras/iris/blob/6776bf0dc9456351a5898fe5299007b19697c8f9/_examples/request-body/read-json-struct-validation/main.go#L2

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):

iris.Version

Additional context

Dexus commented 2 months ago

Hi,

current fix for me is:

var t TypeFromTo
err := ctx.ReadQuery(&t)
if err == nil {
    err = ctx.Application().Validate(t)
}
Dexus commented 2 months ago

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