This repository has been archived on 2025-03-30. You can view files and clone it, but cannot push or open issues or pull requests.
bactdb/types/validation-error.go
Matthew Dillon 708eed5817 json-api error format
Part of #11.
2015-10-14 10:28:09 -07:00

33 lines
561 B
Go

package types
import "encoding/json"
type Source struct {
Pointer string `json:"pointer"`
}
type ErrorDetail struct {
Source `json:"source"`
Detail string `json:"detail"`
}
func NewValidationError(attr, message string) ErrorDetail {
return ErrorDetail{
Source: Source{Pointer: "data/attributes/" + attr},
Detail: message,
}
}
type ValidationError []ErrorDetail
func (v ValidationError) Error() string {
errs, err := json.Marshal(struct {
ValidationError `json:"errors"`
}{v})
if err != nil {
return err.Error()
}
return string(errs)
}