-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_error.go
38 lines (31 loc) · 871 Bytes
/
create_error.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
package laravalidate
import "sort"
func CreateGoError(errors map[string]string) error {
return createCustomError(GoMode, errors)
}
func CreateJsonError(errors map[string]string) error {
return createCustomError(JsonMode, errors)
}
func CreateFormError(errors map[string]string) error {
return createCustomError(FormMode, errors)
}
func createCustomError(mode Mode, errors map[string]string) error {
fieldErrors := []FieldErrors{}
for key, message := range errors {
fieldErrors = append(fieldErrors, FieldErrors{
Path: key,
Errors: []FieldValidatorError{{
Rule: "custom",
Message: message,
}},
})
}
sort.Slice(fieldErrors, func(i, j int) bool {
return fieldErrors[i].Path < fieldErrors[j].Path
})
return &ValidationError{
Mode: mode,
Language: nil, // Maybe we should do something with this?
Errors: fieldErrors,
}
}