Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 174725cf27 | |||
| 45df60e197 | |||
| ecd18128a4 | |||
| f16f5b0116 |
+71
-22
@@ -35,11 +35,11 @@ var hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *
|
||||
}
|
||||
|
||||
var basicTypes = []string{
|
||||
"int", "int64", "float32", "float64", "string", "bool", "uint64",
|
||||
"int", "int16", "int64", "int32", "float32", "float64", "string", "bool", "uint64", "uint32", "uint16",
|
||||
}
|
||||
|
||||
// Verify whether the parameters meet the requirements
|
||||
var validateParams = []string{"require", "pattern", "maxLength", "minLength", "maximum", "minimum"}
|
||||
var validateParams = []string{"require", "pattern", "maxLength", "minLength", "maximum", "minimum", "maxItems", "minItems"}
|
||||
|
||||
// CastError is used for cast type fails
|
||||
type CastError struct {
|
||||
@@ -317,6 +317,10 @@ func DoRequest(request *Request, requestRuntime map[string]interface{}) (respons
|
||||
continue
|
||||
} else if key == "host" {
|
||||
httpRequest.Header["Host"] = []string{*value}
|
||||
delete(httpRequest.Header, "host")
|
||||
} else if key == "user-agent" {
|
||||
httpRequest.Header["User-Agent"] = []string{*value}
|
||||
delete(httpRequest.Header, "user-agent")
|
||||
} else {
|
||||
httpRequest.Header[key] = []string{*value}
|
||||
}
|
||||
@@ -820,12 +824,12 @@ func validateParam(field reflect.StructField, valueField reflect.Value, tagName
|
||||
}
|
||||
}
|
||||
if strings.HasPrefix(field.Type.String(), "[]") { // Verify the parameters of the array type
|
||||
err := validateSlice(valueField, containsTag, tag, tagName)
|
||||
err := validateSlice(field, valueField, containsTag, tag, tagName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if valueField.Kind() == reflect.Ptr { // Determines whether it is a pointer object
|
||||
err := validatePtr(valueField, containsTag, tag, tagName)
|
||||
err := validatePtr(field, valueField, containsTag, tag, tagName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -833,12 +837,28 @@ func validateParam(field reflect.StructField, valueField reflect.Value, tagName
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateSlice(valueField reflect.Value, containsregexpTag bool, tag, tagName string) error {
|
||||
func validateSlice(field reflect.StructField, valueField reflect.Value, containsregexpTag bool, tag, tagName string) error {
|
||||
if valueField.IsValid() && !valueField.IsNil() { // Determines whether the parameter has a value
|
||||
if containsregexpTag {
|
||||
if tagName == "maxItems" {
|
||||
err := checkMaxItems(field, valueField, tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if tagName == "minItems" {
|
||||
err := checkMinItems(field, valueField, tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for m := 0; m < valueField.Len(); m++ {
|
||||
elementValue := valueField.Index(m)
|
||||
if elementValue.Type().Kind() == reflect.Ptr { // Determines whether the child elements of an array are of a basic type
|
||||
err := validatePtr(elementValue, containsregexpTag, tag, tagName)
|
||||
err := validatePtr(field, elementValue, containsregexpTag, tag, tagName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -848,43 +868,42 @@ func validateSlice(valueField reflect.Value, containsregexpTag bool, tag, tagNam
|
||||
return nil
|
||||
}
|
||||
|
||||
func validatePtr(elementValue reflect.Value, containsregexpTag bool, tag, tagName string) error {
|
||||
func validatePtr(field reflect.StructField, elementValue reflect.Value, containsregexpTag bool, tag, tagName string) error {
|
||||
if elementValue.IsNil() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if isFilterType(elementValue.Elem().Type().String(), basicTypes) {
|
||||
if containsregexpTag {
|
||||
if tagName == "pattern" {
|
||||
err := checkPattern(elementValue.Elem(), tag)
|
||||
err := checkPattern(field, elementValue.Elem(), tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if tagName == "maxLength" {
|
||||
err := checkMaxLength(elementValue.Elem(), tag)
|
||||
err := checkMaxLength(field, elementValue.Elem(), tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if tagName == "minLength" {
|
||||
err := checkMinLength(elementValue.Elem(), tag)
|
||||
err := checkMinLength(field, elementValue.Elem(), tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if tagName == "maximum" {
|
||||
err := checkMaximum(elementValue.Elem(), tag)
|
||||
err := checkMaximum(field, elementValue.Elem(), tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if tagName == "minimum" {
|
||||
err := checkMinimum(elementValue.Elem(), tag)
|
||||
err := checkMinimum(field, elementValue.Elem(), tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -909,7 +928,7 @@ func checkRequire(field reflect.StructField, valueField reflect.Value) error {
|
||||
return errors.New(name + " should be setted")
|
||||
}
|
||||
|
||||
func checkPattern(valueField reflect.Value, tag string) error {
|
||||
func checkPattern(field reflect.StructField, valueField reflect.Value, tag string) error {
|
||||
if valueField.IsValid() && valueField.String() != "" {
|
||||
value := valueField.String()
|
||||
if match, _ := regexp.MatchString(tag, value); !match { // Determines whether the parameter value satisfies the regular expression or not, and throws an error
|
||||
@@ -919,7 +938,37 @@ func checkPattern(valueField reflect.Value, tag string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkMaxLength(valueField reflect.Value, tag string) error {
|
||||
func checkMaxItems(field reflect.StructField, valueField reflect.Value, tag string) error {
|
||||
if valueField.IsValid() && valueField.String() != "" {
|
||||
maxItems, err := strconv.Atoi(tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
length := valueField.Len()
|
||||
if maxItems < length {
|
||||
errMsg := fmt.Sprintf("The length of %s is %d which is more than %d", field.Name, length, maxItems)
|
||||
return errors.New(errMsg)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkMinItems(field reflect.StructField, valueField reflect.Value, tag string) error {
|
||||
if valueField.IsValid() {
|
||||
minItems, err := strconv.Atoi(tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
length := valueField.Len()
|
||||
if minItems > length {
|
||||
errMsg := fmt.Sprintf("The length of %s is %d which is less than %d", field.Name, length, minItems)
|
||||
return errors.New(errMsg)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkMaxLength(field reflect.StructField, valueField reflect.Value, tag string) error {
|
||||
if valueField.IsValid() && valueField.String() != "" {
|
||||
maxLength, err := strconv.Atoi(tag)
|
||||
if err != nil {
|
||||
@@ -930,14 +979,14 @@ func checkMaxLength(valueField reflect.Value, tag string) error {
|
||||
length = strings.Count(valueField.String(), "") - 1
|
||||
}
|
||||
if maxLength < length {
|
||||
errMsg := fmt.Sprintf("Length of %s is more than %d", valueField.String(), maxLength)
|
||||
errMsg := fmt.Sprintf("The length of %s is %d which is more than %d", field.Name, length, maxLength)
|
||||
return errors.New(errMsg)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkMinLength(valueField reflect.Value, tag string) error {
|
||||
func checkMinLength(field reflect.StructField, valueField reflect.Value, tag string) error {
|
||||
if valueField.IsValid() {
|
||||
minLength, err := strconv.Atoi(tag)
|
||||
if err != nil {
|
||||
@@ -948,14 +997,14 @@ func checkMinLength(valueField reflect.Value, tag string) error {
|
||||
length = strings.Count(valueField.String(), "") - 1
|
||||
}
|
||||
if minLength > length {
|
||||
errMsg := fmt.Sprintf("Length of %s is less than %d", valueField.String(), minLength)
|
||||
errMsg := fmt.Sprintf("The length of %s is %d which is less than %d", field.Name, length, minLength)
|
||||
return errors.New(errMsg)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkMaximum(valueField reflect.Value, tag string) error {
|
||||
func checkMaximum(field reflect.StructField, valueField reflect.Value, tag string) error {
|
||||
if valueField.IsValid() && valueField.String() != "" {
|
||||
maximum, err := strconv.ParseFloat(tag, 64)
|
||||
if err != nil {
|
||||
@@ -967,14 +1016,14 @@ func checkMaximum(valueField reflect.Value, tag string) error {
|
||||
return err
|
||||
}
|
||||
if maximum < num {
|
||||
errMsg := fmt.Sprintf("%f is greater than %f", num, maximum)
|
||||
errMsg := fmt.Sprintf("The size of %s is %f which is greater than %f", field.Name, num, maximum)
|
||||
return errors.New(errMsg)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkMinimum(valueField reflect.Value, tag string) error {
|
||||
func checkMinimum(field reflect.StructField, valueField reflect.Value, tag string) error {
|
||||
if valueField.IsValid() && valueField.String() != "" {
|
||||
minimum, err := strconv.ParseFloat(tag, 64)
|
||||
if err != nil {
|
||||
@@ -987,7 +1036,7 @@ func checkMinimum(valueField reflect.Value, tag string) error {
|
||||
return err
|
||||
}
|
||||
if minimum > num {
|
||||
errMsg := fmt.Sprintf("%f is less than %f", num, minimum)
|
||||
errMsg := fmt.Sprintf("The size of %s is %f which is less than %f", field.Name, num, minimum)
|
||||
return errors.New(errMsg)
|
||||
}
|
||||
}
|
||||
|
||||
+51
-12
@@ -55,7 +55,9 @@ type validateTest struct {
|
||||
MinLength *errMinLength `json:"MinLength,omitempty"`
|
||||
Maximum *errMaximum `json:"Maximum,omitempty"`
|
||||
Minimum *errMinimum `json:"Minimum,omitempty"`
|
||||
List []*string `json:"list,omitempty" pattern:"^[a-d]*$" maxLength:"4"`
|
||||
MaxItems *errMaxItems `json:"MaxItems,omitempty"`
|
||||
MinItems *errMinItems `json:"MinItems,omitempty"`
|
||||
List []*string `json:"list,omitempty" pattern:"^[a-d]*$" minItems:"2" maxItems:"3" maxLength:"4"`
|
||||
}
|
||||
|
||||
type errMaxLength struct {
|
||||
@@ -74,6 +76,14 @@ type errMinimum struct {
|
||||
Num *int `json:"num" minimum:"a"`
|
||||
}
|
||||
|
||||
type errMaxItems struct {
|
||||
NumMax []*int `json:"num" maxItems:"a"`
|
||||
}
|
||||
|
||||
type errMinItems struct {
|
||||
NumMin []*int `json:"num" minItems:"a"`
|
||||
}
|
||||
|
||||
type Progresstest struct {
|
||||
}
|
||||
|
||||
@@ -373,12 +383,14 @@ func Test_DoRequest(t *testing.T) {
|
||||
|
||||
request.Pathname = String("?log")
|
||||
request.Headers["tea"] = String("")
|
||||
request.Headers["content-length"] = nil
|
||||
runtimeObj["httpsProxy"] = "http://someuser:somepassword@ecs.aliyun.com"
|
||||
resp, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNil(t, resp)
|
||||
utils.AssertEqual(t, `Internal error`, err.Error())
|
||||
|
||||
request.Headers["host"] = String("tea-cn-hangzhou.aliyuncs.com:80")
|
||||
request.Headers["user-agent"] = String("test")
|
||||
resp, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNil(t, resp)
|
||||
utils.AssertEqual(t, `Internal error`, err.Error())
|
||||
@@ -558,6 +570,10 @@ func Test_Validate(t *testing.T) {
|
||||
err = Validate(new(validateTest))
|
||||
utils.AssertEqual(t, err.Error(), "num1 should be setted")
|
||||
|
||||
var tmp *validateTest
|
||||
err = Validate(tmp)
|
||||
utils.AssertNil(t, err)
|
||||
|
||||
err = Validate(nil)
|
||||
utils.AssertNil(t, err)
|
||||
}
|
||||
@@ -584,7 +600,6 @@ func Test_validate(t *testing.T) {
|
||||
Num1: &num,
|
||||
Num2: &num,
|
||||
Str: &str0,
|
||||
List: []*string{&str0},
|
||||
}
|
||||
|
||||
err = validate(reflect.ValueOf(val))
|
||||
@@ -592,7 +607,7 @@ func Test_validate(t *testing.T) {
|
||||
|
||||
val.Str = &str1
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, "Length of abcddd is more than 4", err.Error())
|
||||
utils.AssertEqual(t, "The length of Str is 6 which is more than 4", err.Error())
|
||||
|
||||
val.Num1 = nil
|
||||
err = validate(reflect.ValueOf(val))
|
||||
@@ -604,13 +619,35 @@ func Test_validate(t *testing.T) {
|
||||
|
||||
val.Num1 = &num
|
||||
val.Str = &str0
|
||||
val.List = []*string{&str1}
|
||||
val.List = []*string{&str0}
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, "Length of abcddd is more than 4", err.Error())
|
||||
utils.AssertEqual(t, "The length of List is 1 which is less than 2", err.Error())
|
||||
|
||||
val.Str = nil
|
||||
val.List = []*string{&str0, &str1}
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, "Length of abcddd is more than 4", err.Error())
|
||||
utils.AssertEqual(t, "The length of List is 6 which is more than 4", err.Error())
|
||||
|
||||
val.List = []*string{&str0, &str0}
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertNil(t, err)
|
||||
|
||||
val.MaxItems = &errMaxItems{
|
||||
NumMax: []*int{&num},
|
||||
}
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, `strconv.Atoi: parsing "a": invalid syntax`, err.Error())
|
||||
|
||||
val.MaxItems = nil
|
||||
val.MinItems = &errMinItems{
|
||||
NumMin: []*int{&num},
|
||||
}
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, `strconv.Atoi: parsing "a": invalid syntax`, err.Error())
|
||||
|
||||
val.MinItems = nil
|
||||
val.List = []*string{&str0, &str0, &str0, &str0}
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, "The length of List is 4 which is more than 3", err.Error())
|
||||
|
||||
str2 := "test"
|
||||
val.Str = &str2
|
||||
@@ -625,6 +662,7 @@ func Test_validate(t *testing.T) {
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, `strconv.Atoi: parsing "a": invalid syntax`, err.Error())
|
||||
|
||||
val.List = nil
|
||||
val.MaxLength = nil
|
||||
val.MinLength = &errMinLength{
|
||||
Num: &num,
|
||||
@@ -632,6 +670,7 @@ func Test_validate(t *testing.T) {
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, `strconv.Atoi: parsing "a": invalid syntax`, err.Error())
|
||||
|
||||
val.Name2 = String("tea")
|
||||
val.MinLength = nil
|
||||
val.Maximum = &errMaximum{
|
||||
Num: &num,
|
||||
@@ -649,22 +688,22 @@ func Test_validate(t *testing.T) {
|
||||
val.Minimum = nil
|
||||
val.Num2 = Int(10)
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, `10.000000 is greater than 6.000000`, err.Error())
|
||||
utils.AssertEqual(t, `The size of Num2 is 10.000000 which is greater than 6.000000`, err.Error())
|
||||
|
||||
val.Num2 = nil
|
||||
val.Name1 = String("maxLengthTouch")
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, `Length of maxLengthTouch is more than 4`, err.Error())
|
||||
utils.AssertEqual(t, `The length of Name1 is 14 which is more than 4`, err.Error())
|
||||
|
||||
val.Name1 = nil
|
||||
val.Name2 = String("")
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, `Length of is less than 2`, err.Error())
|
||||
utils.AssertEqual(t, `The length of Name2 is 0 which is less than 2`, err.Error())
|
||||
|
||||
val.Name2 = nil
|
||||
val.Name2 = String("tea")
|
||||
val.Num1 = Int(0)
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, `0.000000 is less than 2.000000`, err.Error())
|
||||
utils.AssertEqual(t, `The size of Num1 is 0.000000 which is less than 2.000000`, err.Error())
|
||||
}
|
||||
|
||||
func Test_Prettify(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user