Golang+Echo: Is there a good way to convert CRLF to LF for context.Bind in case multipart/form-data

842 views Asked by At

I'm use github.com/labstack/echo framefork, and when I retrive data from FE in multipart/form-data, strings contains line breaks as CRLF, but it makes a problem with counting string length, because of CRLF counts as 2 symbols (using utf8.RuneCountInString) it makes problem for validation I'm use (github.com/go-playground/validator). My frontend is also counts symbols in a and validate it, but on FE string have a LF line-breaks, it automatically converts to CRLF for multipart/form-data (by specification)

The github.com/labstack/echo has a way to use [CustomBinder] (https://echo.labstack.com/guide/request/#custom-binder) but I don't know how to use it for convert CRLF to LF, because there is a lot of handler and different structures of data.

Handler example:

func handler(ctx echo.Context) error {
    var data struct {
        SomeField string `validate:"max=512"`
        // a set of other fields
    }

    if err := ctx.Bind(&data); err != nil {
        panic(err)
    }

    if err := ctx.Validate(data); err != nil {
        panic(err)
    }

    utf8.RuneCountInString(data.SomeField) // counts \r\n as 2 symbols
}

CustomBinder example:

type CustomBinder struct {}

func (cb *CustomBinder) Bind(i interface{}, c echo.Context) (err error) {
    // You may use default binder
    db := new(echo.DefaultBinder)
    if err = db.Bind(i, c); err != echo.ErrUnsupportedMediaType {
        return
    }

    // How to convert `CRLF` in all strings in `i` which is interface to `LF`?

    return
}
0

There are 0 answers