~repos /gromer

#golang#htmx#ssr

git clone https://pyrossh.dev/repos/gromer.git

gromer is a framework and cli to build multipage web apps in golang using htmx and alpinejs.


79771120 Peter John

tag: v0.9.6

v0.9.6

3 years ago
improve validation error message
Files changed (2) hide show
  1. http.go +1 -17
  2. utils.go +0 -20
http.go CHANGED
@@ -15,7 +15,6 @@ import (
15
15
 
16
16
  "github.com/go-playground/validator/v10"
17
17
  "github.com/gorilla/mux"
18
- "github.com/iancoleman/strcase"
19
18
  "github.com/rs/zerolog"
20
19
  "github.com/rs/zerolog/log"
21
20
  "github.com/rs/zerolog/pkgerrors"
@@ -48,22 +47,7 @@ func RespondError(w http.ResponseWriter, status int, err error) {
48
47
  }
49
48
  validationErrors, ok := err.(validator.ValidationErrors)
50
49
  if ok {
51
- emap := map[string]string{}
52
- for _, e := range validationErrors {
53
- parts := strings.Split(e.StructNamespace(), ".")
54
- lowerParts := []string{}
55
- for _, p := range parts[1:] {
56
- // TODO: fix pan
57
- lowerParts = append(lowerParts, strcase.ToLowerCamel(p))
58
- }
59
- k := strings.Join(lowerParts, ".")
60
- if e.Tag() == "required" {
61
- emap[k] = "is required"
62
- } else {
63
- emap[k] = e.Error()
64
- }
65
- }
66
- merror["error"] = emap
50
+ merror["error"] = GetValidationError(validationErrors)
67
51
  }
68
52
  data, _ := json.Marshal(merror)
69
53
  w.Write(data)
utils.go CHANGED
@@ -1,10 +1,8 @@
1
1
  package gromer
2
2
 
3
3
  import (
4
- "reflect"
5
4
  "regexp"
6
5
  "strings"
7
- "time"
8
6
 
9
7
  "github.com/go-playground/validator/v10"
10
8
  "github.com/iancoleman/strcase"
@@ -17,24 +15,6 @@ var ValidatorErrorMap = map[string]string{
17
15
  }
18
16
  var upperRegex = regexp.MustCompile("^[^a-z]*$")
19
17
 
20
- type timeTransformer struct {
21
- }
22
-
23
- func (t timeTransformer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error {
24
- if typ == reflect.TypeOf(time.Time{}) {
25
- return func(dst, src reflect.Value) error {
26
- if dst.CanSet() {
27
- srcResult := src.MethodByName("IsZero").Call([]reflect.Value{})
28
- if !srcResult[0].Bool() {
29
- dst.Set(src)
30
- }
31
- }
32
- return nil
33
- }
34
- }
35
- return nil
36
- }
37
-
38
18
  func Merge(dst interface{}, src interface{}) error {
39
19
  err := mergo.Merge(dst, src, mergo.WithOverwriteWithEmptyValue)
40
20
  if err != nil {