monoculum / formam

a package for decode form's values into struct in Go
Apache License 2.0
190 stars 19 forks source link

Add error codes #23

Closed arp242 closed 5 years ago

arp242 commented 5 years ago

The main reason for this is that I want to log (but not ignore) unknown form fields:

err = formam.Decode(r.Form, dst)
if fErr, ok := err.(*formam.Error); ok && fErr.Code() == formam.ErrCodeUnknownField {
    zlog.Error(err)
    err = nil
}
return err

There was no way to get this information previously, and with IgnoreUnknownKeys I would never know if there was an unknown key.

IMHO the IgnoreUnknownKeys option could be deprecated with this, as it's now redundant. But that's a backwards-incompatible change (perhaps something to consider for a v2?)

While I was changing newError(), I also changed it to pass the field and path and build the string with it.

Note: strings.Builder is Go 1.10 and newer only. Previously formam supported a lot of quite old Go versions, and I took the liberty of changing that. I can change it to use string concatenation if you prefer, this is just a bit faster.

emilgpa commented 5 years ago

Thanks you again for this PR! 👍 Look good to me. I think that remove the support to older versions not should be a problem. @markbates what do you think?

arp242 commented 5 years ago

According to https://gobuffalo.io/en, Buffalo requires Go 1.10.8, so should be grand.

Sidenote: you probably want to tag releases as 3.2.0, instead of 3.2. Otherwise Go modules won't pick up on the version numbers, and it gets added as this. Kinda sucks as those patch releases are not all that useful IMHO, but it is what it is...