forked from aws/aws-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic parameter validation support.
Basic parameter validation checks for missing required parameters only. Parameter validation can be disabled with `aws.Config.DisableParamValidation = true`. Fixes aws#1
- Loading branch information
Showing
52 changed files
with
4,019 additions
and
3,804 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
func ValidateParameters(r *Request) { | ||
if r.ParamsFilled() { | ||
v := validator{errors: []string{}} | ||
v.validateAny(reflect.ValueOf(r.Params), "") | ||
|
||
if count := len(v.errors); count > 0 { | ||
format := "%d validation errors:\n- %s" | ||
msg := fmt.Sprintf(format, count, strings.Join(v.errors, "\n- ")) | ||
r.Error = APIError{Code: "InvalidParameter", Message: msg} | ||
} | ||
} | ||
} | ||
|
||
type validator struct { | ||
errors []string | ||
} | ||
|
||
func (v *validator) validateAny(value reflect.Value, path string) { | ||
value = reflect.Indirect(value) | ||
if !value.IsValid() { | ||
return | ||
} | ||
|
||
switch value.Kind() { | ||
case reflect.Struct: | ||
v.validateStruct(value, path) | ||
case reflect.Slice: | ||
for i := 0; i < value.Len(); i++ { | ||
v.validateAny(value.Index(i), path+fmt.Sprintf("[%d]", i)) | ||
} | ||
case reflect.Map: | ||
for _, n := range value.MapKeys() { | ||
v.validateAny(value.MapIndex(n), path+fmt.Sprintf("[%q]", n.String())) | ||
} | ||
} | ||
} | ||
|
||
func (v *validator) validateStruct(value reflect.Value, path string) { | ||
prefix := "." | ||
if path == "" { | ||
prefix = "" | ||
} | ||
|
||
for i := 0; i < value.Type().NumField(); i++ { | ||
f := value.Type().Field(i) | ||
if strings.ToLower(f.Name[0:1]) == f.Name[0:1] { | ||
continue | ||
} | ||
fvalue := value.FieldByName(f.Name) | ||
|
||
notset := false | ||
if f.Tag.Get("required") != "" { | ||
switch fvalue.Kind() { | ||
case reflect.Ptr, reflect.Slice: | ||
if fvalue.IsNil() { | ||
notset = true | ||
} | ||
default: | ||
if !fvalue.IsValid() { | ||
notset = true | ||
} | ||
} | ||
} | ||
|
||
if notset { | ||
msg := "missing required parameter: " + path + prefix + f.Name | ||
v.errors = append(v.errors, msg) | ||
} else { | ||
v.validateAny(fvalue, path+prefix+f.Name) | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package aws_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/awslabs/aws-sdk-go/aws" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var service = func() *aws.Service { | ||
s := &aws.Service{ | ||
Config: &aws.Config{}, | ||
ServiceName: "mock-service", | ||
APIVersion: "2015-01-01", | ||
} | ||
return s | ||
}() | ||
|
||
type StructShape struct { | ||
RequiredList []*ConditionalStructShape `required:"true"` | ||
RequiredMap *map[string]*ConditionalStructShape `required:"true"` | ||
RequiredBool *bool `required:"true"` | ||
OptionalStruct *ConditionalStructShape | ||
|
||
hiddenParameter *string | ||
|
||
metadataStructureShape | ||
} | ||
|
||
type metadataStructureShape struct { | ||
SDKShapeTraits bool | ||
} | ||
|
||
type ConditionalStructShape struct { | ||
Name *string `required:"true"` | ||
SDKShapeTraits bool | ||
} | ||
|
||
func TestNoErrors(t *testing.T) { | ||
input := &StructShape{ | ||
RequiredList: []*ConditionalStructShape{}, | ||
RequiredMap: &map[string]*ConditionalStructShape{ | ||
"key1": &ConditionalStructShape{Name: aws.String("Name")}, | ||
"key2": &ConditionalStructShape{Name: aws.String("Name")}, | ||
}, | ||
RequiredBool: aws.Boolean(true), | ||
OptionalStruct: &ConditionalStructShape{Name: aws.String("Name")}, | ||
} | ||
|
||
req := aws.NewRequest(service, &aws.Operation{}, input, nil) | ||
aws.ValidateParameters(req) | ||
assert.NoError(t, req.Error) | ||
} | ||
|
||
func TestMissingRequiredParameters(t *testing.T) { | ||
input := &StructShape{} | ||
req := aws.NewRequest(service, &aws.Operation{}, input, nil) | ||
aws.ValidateParameters(req) | ||
err := aws.Error(req.Error) | ||
|
||
assert.Error(t, err) | ||
assert.Equal(t, "InvalidParameter", err.Code) | ||
assert.Equal(t, "3 validation errors:\n- missing required parameter: RequiredList\n- missing required parameter: RequiredMap\n- missing required parameter: RequiredBool", err.Message) | ||
} | ||
|
||
func TestNestedMissingRequiredParameters(t *testing.T) { | ||
input := &StructShape{ | ||
RequiredList: []*ConditionalStructShape{&ConditionalStructShape{}}, | ||
RequiredMap: &map[string]*ConditionalStructShape{ | ||
"key1": &ConditionalStructShape{Name: aws.String("Name")}, | ||
"key2": &ConditionalStructShape{}, | ||
}, | ||
RequiredBool: aws.Boolean(true), | ||
OptionalStruct: &ConditionalStructShape{}, | ||
} | ||
|
||
req := aws.NewRequest(service, &aws.Operation{}, input, nil) | ||
aws.ValidateParameters(req) | ||
err := aws.Error(req.Error) | ||
|
||
assert.Error(t, err) | ||
assert.Equal(t, "InvalidParameter", err.Code) | ||
assert.Equal(t, "3 validation errors:\n- missing required parameter: RequiredList[0].Name\n- missing required parameter: RequiredMap[\"key2\"].Name\n- missing required parameter: OptionalStruct.Name", err.Message) | ||
|
||
} |
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
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
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
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
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
Oops, something went wrong.