Merge pull request #2466 from TimeIncOSS/f-schema-field-name-validate

schema: Add field name to ValidateFunc
This commit is contained in:
Radek Simko 2015-06-24 18:52:53 +01:00
commit 6fdbca8e58
3 changed files with 12 additions and 12 deletions

View File

@ -154,17 +154,17 @@ func resourceAwsDbInstance() *schema.Resource {
"final_snapshot_identifier": &schema.Schema{ "final_snapshot_identifier": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
ValidateFunc: func(v interface{}) (ws []string, es []error) { ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
fsi := v.(string) fsi := v.(string)
if !regexp.MustCompile(`^[0-9A-Za-z-]+$`).MatchString(fsi) { if !regexp.MustCompile(`^[0-9A-Za-z-]+$`).MatchString(fsi) {
es = append(es, fmt.Errorf( es = append(es, fmt.Errorf(
"only alphanumeric characters and hyphens allowed")) "only alphanumeric characters and hyphens allowed in %s", k))
} }
if regexp.MustCompile(`--`).MatchString(fsi) { if regexp.MustCompile(`--`).MatchString(fsi) {
es = append(es, fmt.Errorf("cannot contain two consecutive hyphens")) es = append(es, fmt.Errorf("%s cannot contain two consecutive hyphens", k))
} }
if regexp.MustCompile(`-$`).MatchString(fsi) { if regexp.MustCompile(`-$`).MatchString(fsi) {
es = append(es, fmt.Errorf("cannot end in a hyphen")) es = append(es, fmt.Errorf("%s cannot end in a hyphen", k))
} }
return return
}, },

View File

@ -183,7 +183,7 @@ type SchemaStateFunc func(interface{}) string
// SchemaValidateFunc is a function used to validate a single field in the // SchemaValidateFunc is a function used to validate a single field in the
// schema. // schema.
type SchemaValidateFunc func(interface{}) ([]string, []error) type SchemaValidateFunc func(interface{}, string) ([]string, []error)
func (s *Schema) GoString() string { func (s *Schema) GoString() string {
return fmt.Sprintf("*%#v", *s) return fmt.Sprintf("*%#v", *s)
@ -1178,7 +1178,7 @@ func (m schemaMap) validatePrimitive(
} }
if schema.ValidateFunc != nil { if schema.ValidateFunc != nil {
return schema.ValidateFunc(decoded) return schema.ValidateFunc(decoded, k)
} }
return nil, nil return nil, nil

View File

@ -2803,7 +2803,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
"foo": &Schema{ "foo": &Schema{
Type: TypeMap, Type: TypeMap,
Required: true, Required: true,
ValidateFunc: func(v interface{}) (ws []string, es []error) { ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
return return
}, },
}, },
@ -3445,7 +3445,7 @@ func TestSchemaMap_Validate(t *testing.T) {
"validate_me": &Schema{ "validate_me": &Schema{
Type: TypeString, Type: TypeString,
Required: true, Required: true,
ValidateFunc: func(v interface{}) (ws []string, es []error) { ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
return return
}, },
}, },
@ -3461,7 +3461,7 @@ func TestSchemaMap_Validate(t *testing.T) {
"validate_me": &Schema{ "validate_me": &Schema{
Type: TypeString, Type: TypeString,
Required: true, Required: true,
ValidateFunc: func(v interface{}) (ws []string, es []error) { ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
es = append(es, fmt.Errorf("something is not right here")) es = append(es, fmt.Errorf("something is not right here"))
return return
}, },
@ -3481,7 +3481,7 @@ func TestSchemaMap_Validate(t *testing.T) {
"number": &Schema{ "number": &Schema{
Type: TypeInt, Type: TypeInt,
Required: true, Required: true,
ValidateFunc: func(v interface{}) (ws []string, es []error) { ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
t.Fatalf("Should not have gotten validate call") t.Fatalf("Should not have gotten validate call")
return return
}, },
@ -3498,7 +3498,7 @@ func TestSchemaMap_Validate(t *testing.T) {
"maybe": &Schema{ "maybe": &Schema{
Type: TypeBool, Type: TypeBool,
Required: true, Required: true,
ValidateFunc: func(v interface{}) (ws []string, es []error) { ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
if _, ok := v.(bool); !ok { if _, ok := v.(bool); !ok {
t.Fatalf("Expected bool, got: %#v", v) t.Fatalf("Expected bool, got: %#v", v)
} }
@ -3516,7 +3516,7 @@ func TestSchemaMap_Validate(t *testing.T) {
"validate_me": &Schema{ "validate_me": &Schema{
Type: TypeString, Type: TypeString,
Required: true, Required: true,
ValidateFunc: func(v interface{}) (ws []string, es []error) { ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
es = append(es, fmt.Errorf("something is not right here")) es = append(es, fmt.Errorf("something is not right here"))
return return
}, },