EUGENIO SOUZA CARVALHO 4 年 前
コミット
463093b28f
1 ファイル変更34 行追加6 行削除
  1. 34 6
      common/models.go

+ 34 - 6
common/models.go

@@ -408,21 +408,49 @@ var (
 
 	validationParamFunctions = map[string]func(interface{}, interface{}) *errs.Error{
 		"min": func(value interface{}, minString interface{}) *errs.Error {
-			min, err := strconv.Atoi(minString.(string))
-			if err != nil || value.(int) < min {
+			var input float64
+
+			if v, ok := value.(int64); ok {
+				input = float64(v)
+			} else if v, ok := value.(float64); ok {
+				input = v
+			} else if v, ok := value.(string); ok {
+				input = float64(len(v))
+			} else {
 				invalidArgument := errs.InvalidArgument()
-				invalidArgument.Message = fmt.Sprintf("ValueRestriction: value > %s. Received (%d)", minString, value)
+				invalidArgument.Message = fmt.Sprintf("ValueRestriction: mim validation requires (int,float,string)")
+				return invalidArgument
+			}
+
+			if min, convert := minString.(float64); !convert || input < min {
+				invalidArgument := errs.InvalidArgument()
+				invalidArgument.Message = fmt.Sprintf("ValueRestriction: value > %v. Received (%v)", minString, value)
 				return invalidArgument
 			}
 			return nil
 		},
 		"max": func(value interface{}, maxString interface{}) *errs.Error {
-			max, err := strconv.Atoi(maxString.(string))
-			if err != nil || value.(int) > max {
+
+			var input float64
+
+			if v, ok := value.(int64); ok {
+				input = float64(v)
+			} else if v, ok := value.(float64); ok {
+				input = v
+			} else if v, ok := value.(string); ok {
+				input = float64(len(v))
+			} else {
 				invalidArgument := errs.InvalidArgument()
-				invalidArgument.Message = fmt.Sprintf("ValueRestriction: value < %s. Received (%d)", maxString, value)
+				invalidArgument.Message = fmt.Sprintf("ValueRestriction: mim validation requires (int,float,string)")
 				return invalidArgument
 			}
+
+			if max, convert := maxString.(float64); !convert || input > max {
+				invalidArgument := errs.InvalidArgument()
+				invalidArgument.Message = fmt.Sprintf("ValueRestriction: value < %v. Received (%v)", maxString, value)
+				return invalidArgument
+			}
+
 			return nil
 		},
 		"accept": func(input interface{}, accept interface{}) *errs.Error {