[FEATURE REQUEST] Describe more specific error for PostValueBool, PostValueInt & friends
- Dominant language
- Go
- Stars
- 25.6k
- Forks
- 2.4k
- PR merge metrics
- No merged PRs in 30d
Description
Hi,
AFAIK, in a RESTful API design, we can specify update only for specific fields. For example, say that I have items with these following fields:
- `name`
- `description`
- `price`
- `tags`
Then I want to only update `price`. So I specify `price=1000` in request body and leave other fields on its initial values.
But other important behavior that is needed is, if I specify `tags` with empty value `""`, it should be able to *removes* tags of that item.
The problem is, this line of code throws same error message if `price` field is unset **OR** `price` field has empty value **OR** `price` have invalid value.
```
price, err := ctx.PostValueInt("price")
```
The other thing is, for best practice, RESTful API must be able to response an error if unexpected field(s) was specified inside a request body. Currently I done checking with something like this:
```
allowedKeys := []string{"name", "description", "price", "tags"}
formData := c.FormValues()
//Make sure all key are valid
for formKey := range formData {
isValid := false
for _, allowedKey := range allowedKeys {
if formKey == allowedKey {
isValid = true
break
}
}
if isValid == false {
return result, helper.NewRequestError("VALIDATION_ERROR", fmt.Sprintf("Unexpected field '%s'", formKey))
}
}
```
And I select defined values to update with something like this:
```
valuesToUpdate := make(map[string]interface{})
if val, ok := formData["name"]; ok {
valuesToUpdate["name"] = val
}
if _, ok := formData["price"]; ok {
if price, err := c.PostValueInt("price"); err == nil {
valuesToUpdate["price"] = price
} else {
return result, helper.NewRequestError("VALIDATION_ERROR", "Invalid price format")
}
}
```
I think it would be great and simple if `PostValueInt()`, `PostValueBool()`, etc; can inform us detailed error type.
Furthermore, maybe we also need some kind of these functions too.
- `func PostValueString(string) (string, error)`
- `func PostValuesList() (map[string][]string, error) // without the URL field's query parameters`
What do you think?
Contributor guide
Assessment
This issue has not been assessed yet.