-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidationproblem.go
65 lines (53 loc) · 1.8 KB
/
validationproblem.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package problemdetails
import (
"github.com/go-playground/validator/v10"
)
type ValidationError struct {
Field string
Error string
}
type ValidationProblem struct {
Type string `json:"type"` // url to the problem type
Title string `json:"title"` // problem title
Status int `json:"status,omitempty"` // http status code
Detail string `json:"detail,omitempty"` // a human-readable explaination
Instance string `json:"instance,omitempty"` // A URI reference that identifies the specific ocurrence. Does not need to derefrence to something
// Fields added by VQ
ValidationErrors []ValidationError `json:"validationerrors,omitempty"` // a list of the validation errors that occurred
}
func (problem *ValidationProblem) WithType(t string) *ValidationProblem {
problem.Type = t
return problem
}
func (problem *ValidationProblem) WithTitle(title string) *ValidationProblem {
problem.Title = title
return problem
}
func (problem *ValidationProblem) WithStatus(status int) *ValidationProblem {
problem.Status = status
return problem
}
func (problem *ValidationProblem) WithDetail(detail string) *ValidationProblem {
problem.Detail = detail
return problem
}
func (problem *ValidationProblem) WithInstance(instance string) *ValidationProblem {
problem.Instance = instance
return problem
}
func FromValidationErrors(validationErrors validator.ValidationErrors) ValidationProblem {
var errs []ValidationError
for _, e := range validationErrors {
errs = append(errs, ValidationError{
Field: e.Field(),
Error: e.Error(),
})
}
return ValidationProblem{
// TODO: Add status code handling here later on, for now about:blank is fine by the RFC
Type: "about:blank",
Status: 400,
Detail: "Invalid request",
ValidationErrors: errs,
}
}