|
| 1 | +package validation |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +var validators = map[string]interface{}{ |
| 11 | + "required": validateRequired, |
| 12 | + "minlength": validateMinLength, |
| 13 | + "uuid": validationIsUUID, |
| 14 | + "email": validateIsEmail, |
| 15 | + "numeric": validateIsNumber, |
| 16 | + "between": validationBetween, |
| 17 | + "minmax": validationMinMaxNumber, |
| 18 | +} |
| 19 | + |
| 20 | +var data = map[string]map[string]string{ |
| 21 | + "required": { |
| 22 | + "error_msg": "%s field is required.", |
| 23 | + }, |
| 24 | + "minlength": { |
| 25 | + "error_msg": "%s must be at least %d characters long.", |
| 26 | + }, |
| 27 | + "uuid": { |
| 28 | + "error_msg": "%s must be a valid UUID.", |
| 29 | + }, |
| 30 | + "email": { |
| 31 | + "error_msg": "%s must be a valid email address.", |
| 32 | + }, |
| 33 | + "numeric": { |
| 34 | + "error_msg": "%s must be a number.", |
| 35 | + }, |
| 36 | + "between": { |
| 37 | + "error_msg": "%s must be between %d and %d characters long.", |
| 38 | + }, |
| 39 | + "minmax": { |
| 40 | + "error_msg": "%s must be between %d and %d.", |
| 41 | + }, |
| 42 | +} |
| 43 | + |
| 44 | +func ValidateStruct(s interface{}) error { |
| 45 | + v := reflect.ValueOf(s) |
| 46 | + t := v.Type() |
| 47 | + for i := 0; i < t.NumField(); i++ { |
| 48 | + field := t.Field(i) |
| 49 | + value := v.Field(i) |
| 50 | + fieldName := field.Name |
| 51 | + validateTag := field.Tag.Get("validate") |
| 52 | + data := strings.Split(validateTag, ",") |
| 53 | + for _, d := range data { |
| 54 | + parts := strings.SplitN(d, "=", 2) |
| 55 | + tagName := parts[0] |
| 56 | + var tagParam string |
| 57 | + if len(parts) > 1 { |
| 58 | + tagParam = parts[1] |
| 59 | + } |
| 60 | + if validator, exists := validators[tagName]; exists { |
| 61 | + switch validator := validator.(type) { |
| 62 | + case func(string, string) error: |
| 63 | + errorMsg := getMessage(tagName, fieldName) |
| 64 | + if err := validator(fmt.Sprint(value), errorMsg); err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + case func(string, int, string) error: |
| 68 | + if param, err := strconv.Atoi(tagParam); err == nil { |
| 69 | + errorMsg := getMessage(tagName, fieldName, param) |
| 70 | + if err := validator(value.String(), param, errorMsg); err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + } else { |
| 74 | + return fmt.Errorf("invalid parameter for tag %s", tagName) |
| 75 | + } |
| 76 | + case func(string, int, int, string) error: |
| 77 | + params := strings.Split(tagParam, "-") |
| 78 | + if len(params) != 2 { |
| 79 | + return fmt.Errorf("invalid parameter for tag %s", tagName) |
| 80 | + } |
| 81 | + min, err := strconv.Atoi(params[0]) |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("invalid parameter for tag %s", tagName) |
| 84 | + } |
| 85 | + |
| 86 | + max, err := strconv.Atoi(params[1]) |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("invalid parameter for tag %s", tagName) |
| 89 | + } |
| 90 | + errorMsg := getMessage(tagName, fieldName, min, max) |
| 91 | + if err := validator(fmt.Sprint(value), min, max, errorMsg); err != nil { |
| 92 | + return err |
| 93 | + } else { |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +func getMessage(tagName, fieldName string, args ...interface{}) string { |
| 105 | + errorMsgTemplate := data[tagName]["error_msg"] |
| 106 | + return fmt.Sprintf(errorMsgTemplate, append([]interface{}{fieldName}, args...)...) |
| 107 | +} |
0 commit comments