|
@@ -364,14 +364,24 @@ func RequestParams(args string, params map[string]*Parameter) func(ctx context.C
|
|
|
|
|
|
if param.Required && (value == "" || value == nil) {
|
|
if param.Required && (value == "" || value == nil) {
|
|
invalidArgument := errs.InvalidArgument()
|
|
invalidArgument := errs.InvalidArgument()
|
|
- invalidArgument.Message = fmt.Sprintf("ParamRequired:'%s'", param.ID)
|
|
|
|
|
|
+ invalidArgument.Message = fmt.Sprintf(
|
|
|
|
+ "ParamRequired: param '%s' in '%s'",
|
|
|
|
+ param.ID,
|
|
|
|
+ param.Location,
|
|
|
|
+ )
|
|
return nil, invalidArgument
|
|
return nil, invalidArgument
|
|
}
|
|
}
|
|
|
|
|
|
if param.ConvertTo != "" {
|
|
if param.ConvertTo != "" {
|
|
if value, err = convertValueByType(param.ConvertTo, value); err != nil {
|
|
if value, err = convertValueByType(param.ConvertTo, value); err != nil {
|
|
invalidArgument := errs.InvalidArgument()
|
|
invalidArgument := errs.InvalidArgument()
|
|
- invalidArgument.Message = fmt.Sprintf("TypeConversionError:'%v'. Waiting a %s ", value, param.ConvertTo)
|
|
|
|
|
|
+ invalidArgument.Message = fmt.Sprintf(
|
|
|
|
+ "ParamTypeConversionError: param '%s' in '%s' with value '%v'. Waiting a %s ",
|
|
|
|
+ param.ID,
|
|
|
|
+ param.Location,
|
|
|
|
+ value,
|
|
|
|
+ param.ConvertTo,
|
|
|
|
+ )
|
|
return nil, invalidArgument
|
|
return nil, invalidArgument
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -379,10 +389,8 @@ func RequestParams(args string, params map[string]*Parameter) func(ctx context.C
|
|
if param.Validation != nil {
|
|
if param.Validation != nil {
|
|
for validator, args := range param.Validation {
|
|
for validator, args := range param.Validation {
|
|
if fn, found := validationParamFunctions[validator]; found {
|
|
if fn, found := validationParamFunctions[validator]; found {
|
|
-
|
|
|
|
ctx.Application().Logger().Info(fmt.Sprintf("validadete[%s][%s][%v]", validator, args, value))
|
|
ctx.Application().Logger().Info(fmt.Sprintf("validadete[%s][%s][%v]", validator, args, value))
|
|
-
|
|
|
|
- if err = fn(value, args); err != nil {
|
|
|
|
|
|
+ if err = fn(param, value, args); err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -406,8 +414,8 @@ var (
|
|
"number": stringToFloat,
|
|
"number": stringToFloat,
|
|
}
|
|
}
|
|
|
|
|
|
- validationParamFunctions = map[string]func(interface{}, interface{}) *errs.Error{
|
|
|
|
- "min": func(value interface{}, minString interface{}) *errs.Error {
|
|
|
|
|
|
+ validationParamFunctions = map[string]func(*Parameter,interface{}, interface{}) *errs.Error{
|
|
|
|
+ "min": func(param *Parameter, value interface{}, minString interface{}) *errs.Error {
|
|
var input float64
|
|
var input float64
|
|
|
|
|
|
if v, ok := value.(int64); ok {
|
|
if v, ok := value.(int64); ok {
|
|
@@ -418,18 +426,26 @@ var (
|
|
input = float64(len(v))
|
|
input = float64(len(v))
|
|
} else {
|
|
} else {
|
|
invalidArgument := errs.InvalidArgument()
|
|
invalidArgument := errs.InvalidArgument()
|
|
- invalidArgument.Message = fmt.Sprintf("ValueRestriction: mim validation requires (int,float,string)")
|
|
|
|
|
|
+ invalidArgument.Message = fmt.Sprintf(
|
|
|
|
+ "[%s] ValueRestriction: mim validation requires (int,float,string)",
|
|
|
|
+ param.ID,
|
|
|
|
+ )
|
|
return invalidArgument
|
|
return invalidArgument
|
|
}
|
|
}
|
|
|
|
|
|
if min, convert := minString.(float64); !convert || input < min {
|
|
if min, convert := minString.(float64); !convert || input < min {
|
|
invalidArgument := errs.InvalidArgument()
|
|
invalidArgument := errs.InvalidArgument()
|
|
- invalidArgument.Message = fmt.Sprintf("ValueRestriction: value > %v. Received (%v)", minString, value)
|
|
|
|
|
|
+ invalidArgument.Message = fmt.Sprintf(
|
|
|
|
+ "[%s] ValueRestriction: value > %v. Received (%v)",
|
|
|
|
+ param.ID,
|
|
|
|
+ minString,
|
|
|
|
+ value,
|
|
|
|
+ )
|
|
return invalidArgument
|
|
return invalidArgument
|
|
}
|
|
}
|
|
return nil
|
|
return nil
|
|
},
|
|
},
|
|
- "max": func(value interface{}, maxString interface{}) *errs.Error {
|
|
|
|
|
|
+ "max": func(param *Parameter, value interface{}, maxString interface{}) *errs.Error {
|
|
|
|
|
|
var input float64
|
|
var input float64
|
|
|
|
|
|
@@ -441,19 +457,27 @@ var (
|
|
input = float64(len(v))
|
|
input = float64(len(v))
|
|
} else {
|
|
} else {
|
|
invalidArgument := errs.InvalidArgument()
|
|
invalidArgument := errs.InvalidArgument()
|
|
- invalidArgument.Message = fmt.Sprintf("ValueRestriction: mim validation requires (int,float,string)")
|
|
|
|
|
|
+ invalidArgument.Message = fmt.Sprintf(
|
|
|
|
+ "[%s] ValueRestriction: mim validation requires (int,float,string)",
|
|
|
|
+ param.ID,
|
|
|
|
+ )
|
|
return invalidArgument
|
|
return invalidArgument
|
|
}
|
|
}
|
|
|
|
|
|
if max, convert := maxString.(float64); !convert || input > max {
|
|
if max, convert := maxString.(float64); !convert || input > max {
|
|
invalidArgument := errs.InvalidArgument()
|
|
invalidArgument := errs.InvalidArgument()
|
|
- invalidArgument.Message = fmt.Sprintf("ValueRestriction: value < %v. Received (%v)", maxString, value)
|
|
|
|
|
|
+ invalidArgument.Message = fmt.Sprintf(
|
|
|
|
+ "[%s] ValueRestriction: value < %v. Received (%v)",
|
|
|
|
+ param.ID,
|
|
|
|
+ maxString,
|
|
|
|
+ value,
|
|
|
|
+ )
|
|
return invalidArgument
|
|
return invalidArgument
|
|
}
|
|
}
|
|
|
|
|
|
return nil
|
|
return nil
|
|
},
|
|
},
|
|
- "accept": func(input interface{}, accept interface{}) *errs.Error {
|
|
|
|
|
|
+ "accept": func(param *Parameter, input interface{}, accept interface{}) *errs.Error {
|
|
var (
|
|
var (
|
|
acceptValues = accept.([]interface{})
|
|
acceptValues = accept.([]interface{})
|
|
acceptValuesString = []string{}
|
|
acceptValuesString = []string{}
|
|
@@ -469,13 +493,14 @@ var (
|
|
|
|
|
|
invalidArgument := errs.InvalidArgument()
|
|
invalidArgument := errs.InvalidArgument()
|
|
invalidArgument.Message = fmt.Sprintf(
|
|
invalidArgument.Message = fmt.Sprintf(
|
|
- "ValueRestriction: '%s' isn't accept. Accept [%s]",
|
|
|
|
|
|
+ "[%s] ValueRestriction: '%s' isn't accept. Accept [%s]",
|
|
|
|
+ param.ID,
|
|
value,
|
|
value,
|
|
strings.Join(acceptValuesString, ","),
|
|
strings.Join(acceptValuesString, ","),
|
|
)
|
|
)
|
|
return invalidArgument
|
|
return invalidArgument
|
|
},
|
|
},
|
|
- "reject": func(input interface{}, reject interface{}) *errs.Error {
|
|
|
|
|
|
+ "reject": func(param *Parameter, input interface{}, reject interface{}) *errs.Error {
|
|
var (
|
|
var (
|
|
rejectValues = reject.([]interface{})
|
|
rejectValues = reject.([]interface{})
|
|
value = fmt.Sprintf("%v", input)
|
|
value = fmt.Sprintf("%v", input)
|
|
@@ -485,7 +510,8 @@ var (
|
|
if value == rejectValue.(string) {
|
|
if value == rejectValue.(string) {
|
|
invalidArgument := errs.InvalidArgument()
|
|
invalidArgument := errs.InvalidArgument()
|
|
invalidArgument.Message = fmt.Sprintf(
|
|
invalidArgument.Message = fmt.Sprintf(
|
|
- "ValueRestriction: '%s' isn't accept",
|
|
|
|
|
|
+ "[%s] ValueRestriction: '%s' isn't accept",
|
|
|
|
+ param.ID,
|
|
value,
|
|
value,
|
|
)
|
|
)
|
|
return invalidArgument
|
|
return invalidArgument
|
|
@@ -493,7 +519,7 @@ var (
|
|
}
|
|
}
|
|
return nil
|
|
return nil
|
|
},
|
|
},
|
|
- "regex": func(input interface{}, regex interface{}) *errs.Error {
|
|
|
|
|
|
+ "regex": func(param *Parameter, input interface{}, regex interface{}) *errs.Error {
|
|
var (
|
|
var (
|
|
regexString = regex.(string)
|
|
regexString = regex.(string)
|
|
value = input.(string)
|
|
value = input.(string)
|
|
@@ -505,7 +531,8 @@ var (
|
|
|
|
|
|
invalidArgument := errs.InvalidArgument()
|
|
invalidArgument := errs.InvalidArgument()
|
|
invalidArgument.Message = fmt.Sprintf(
|
|
invalidArgument.Message = fmt.Sprintf(
|
|
- "ValueRestriction: '%s' isn't accept",
|
|
|
|
|
|
+ "[%s] ValueRestriction: '%s' isn't accept",
|
|
|
|
+ param.ID,
|
|
value,
|
|
value,
|
|
)
|
|
)
|
|
|
|
|