/
validate.go
60 lines (51 loc) · 1.42 KB
/
validate.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
package graphql
import (
"context"
"errors"
"strings"
"unicode"
"github.com/99designs/gqlgen/graphql"
"github.com/go-playground/validator/v10"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/gqlerror"
)
// ValidateStructAndAppendErrors a struct and appends errors, if there is one, to response.
func ValidateStructAndAppendErrors(ctx context.Context, validate *validator.Validate, obj interface{}, argName string) error {
err := validate.Struct(obj)
if err == nil {
return nil
}
if errs := err.(validator.ValidationErrors); errs != nil {
for _, err := range errs {
// Recreate the rule that failed.
rule := err.ActualTag()
param := err.Param()
if param != "" {
rule += "=" + param
}
// Recreate graphql path for input argument.
path := append(graphql.GetPath(ctx), ast.PathName(argName))
namespace := strings.Split(err.Namespace(), ".")[1:]
for _, name := range namespace {
path = append(path, ast.PathName(makeFirstCharLowercase(name)))
}
// Add error to response.
graphql.AddError(ctx, &gqlerror.Error{
Path: path,
Message: "input validation failed",
Extensions: map[string]interface{}{
"code": InputValidationError,
"value": err.Value(),
"rule": rule,
},
})
}
}
return errors.New("")
}
func makeFirstCharLowercase(s string) string {
for _, v := range s {
return string(unicode.ToLower(v)) + s[1:]
}
return s
}