bug: required errors use golang name instead of field name
davidalpert opened this issue · 3 comments
Given a type with required fields:
type ProductQuery struct {
CreatedAt time.Time `in:"form=created_at;required"`
Color string `in:"form=colour,color"`
IsSoldout bool `in:"form=is_soldout"`
SortBy []string `in:"form=sort_by"`
SortDesc []bool `in:"form=sort_desc"`
Pagination
Authorization
}
When I make a request to an endpoint parsing this type with httpin
Then I expect the error to be
invalid field "created_at": missing required field
Currently the error is
invalid field "CreatedAt": missing required field
which can be misleading to clients, since submitting a request with a CreatedAt
property is not guaranteed to bind.
Thanks @davidalpert
This was an intended design. In httpin we allow applying multiple directives to a field. And at the same time for a directive, it also can have multiple args. For instance:
Token string `in:"form=access_token,token;header=x-api-token;required"`
IMO, it's too complex to name the field by concating the directives together to indicate which field had an error.
I agree with the point that currently the error message is missleading. But compared with the solution #9 had provided I propose if we could add more "contexts" to InvalidFieldError
to make it more clear.
For example, just expose the Directives
:
type InvalidFieldError struct {
// Field is the name of the field.
Field string `json:"field"`
// Source is the directive which causes the error.
// e.g. form, header, required, etc.
Source string `json:"source"`
// Value is the input data.
Value interface{} `json:"value"`
// internalError is the underlying error thrown by the directive executor.
internalError error `json:"-"`
ErrorMessage string `json:"error"`
// The directives bound to this field. <------ here :)
Directives []*Directive `json:"directives"`
}
The advantage here is we can access the directives data in a structured way. Which should be easier for someone if they want to tweak InvalidFieldError
before sending to the client.
It's not covenient to tweak the error before sending to the client in the default middleware handler httpin.NewInput
@ggicci
Hi @davidalpert , now the bound Directives has been exposed to InvalidationFieldError
, and overriding the default error handler has also been supported.
Closed and feel free to reopen it :)