Created
October 4, 2018 10:22
-
-
Save mishazapl/fde0d85c6fa78bf28b386642a3f2a854 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Validation | |
import ( | |
"mini-project/app/config" | |
"strconv" | |
) | |
type Rule struct{} | |
func (r Rule) Required(validation Validation, value string, key string) Validation { | |
if value == "" { | |
current := validation.ErrorValidation[key] | |
validation.ErrorValidation["error"+key] = current + " Поле должно быть заполнено." | |
} | |
return validation | |
} | |
func (r Rule) Min(validation Validation, value string, key string, limit int) Validation { | |
if len(value) < limit { | |
current := validation.ErrorValidation[key] | |
validation.ErrorValidation["error"+key] = current + " Поле должно быть больше " + strconv.Itoa(limit) + "." | |
} | |
return validation | |
} | |
func (r Rule) Max(validation Validation, value string, key string, limit int) Validation { | |
if len(value) > limit { | |
current := validation.ErrorValidation[key] | |
validation.ErrorValidation["error"+key] = current + " Поле должно быть меньше " + strconv.Itoa(limit) + "." | |
} | |
return validation | |
} | |
func (r Rule) Unique(validation Validation, value string, key string, table string, field string) Validation { | |
var count int | |
config.DB.Table(table).Where(field+" = ?", value).Count(&count) | |
if count > 0 { | |
current := validation.ErrorValidation[key] | |
validation.ErrorValidation["error"+key] = current + "cо значением " + value + " уже зарегистрирован пользователь. " | |
} | |
return validation | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Validation | |
import ( | |
"reflect" | |
"strings" | |
"strconv" | |
) | |
type Validation struct { | |
ErrorValidation map[string]string | |
} | |
var ValidationStruct Validation | |
func (v Validation) Validate(user reflect.Value, rule map[string]string) Validation { | |
ValidationStruct.ErrorValidation = make(map[string]string) | |
for value, key := range rule { | |
for _, val := range strings.Split(key, "|") { | |
v.CallRule(val, ValidationStruct, user.FieldByName(value).String(), value) | |
} | |
} | |
return ValidationStruct | |
} | |
func (v Validation) CallRule(rule string, validation Validation, value string, key string) { | |
ruleSeparator := strings.Split(rule, ":") | |
switch ruleSeparator[0] { | |
case "Required": | |
ValidationStruct = Rule{}.Required(validation, value, key) | |
case "Max": | |
limit, _ := strconv.ParseInt(ruleSeparator[1], 10, 64) | |
ValidationStruct = Rule{}.Max(validation, value, key, int(limit)) | |
case "Min": | |
limit, _ := strconv.ParseInt(ruleSeparator[1], 10, 64) | |
ValidationStruct = Rule{}.Min(validation, value, key, int(limit)) | |
case "Unique": | |
tableField := strings.Split(ruleSeparator[1], ",") | |
ValidationStruct = Rule{}.Unique(validation, value, key, tableField[0], tableField[1]) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment