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{
Type: schema.TypeString,
Optional: true,
ValidateFunc: func(v interface{}) (ws []string, es []error) {
ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
fsi := v.(string)
if !regexp.MustCompile(`^[0-9A-Za-z-]+$`).MatchString(fsi) {
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) {
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) {
es = append(es, fmt.Errorf("cannot end in a hyphen"))
es = append(es, fmt.Errorf("%s cannot end in a hyphen", k))
}
return
},

View File

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

View File

@ -2803,7 +2803,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
"foo": &Schema{
Type: TypeMap,
Required: true,
ValidateFunc: func(v interface{}) (ws []string, es []error) {
ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
return
},
},
@ -3445,7 +3445,7 @@ func TestSchemaMap_Validate(t *testing.T) {
"validate_me": &Schema{
Type: TypeString,
Required: true,
ValidateFunc: func(v interface{}) (ws []string, es []error) {
ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
return
},
},
@ -3461,7 +3461,7 @@ func TestSchemaMap_Validate(t *testing.T) {
"validate_me": &Schema{
Type: TypeString,
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"))
return
},
@ -3481,7 +3481,7 @@ func TestSchemaMap_Validate(t *testing.T) {
"number": &Schema{
Type: TypeInt,
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")
return
},
@ -3498,7 +3498,7 @@ func TestSchemaMap_Validate(t *testing.T) {
"maybe": &Schema{
Type: TypeBool,
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 {
t.Fatalf("Expected bool, got: %#v", v)
}
@ -3516,7 +3516,7 @@ func TestSchemaMap_Validate(t *testing.T) {
"validate_me": &Schema{
Type: TypeString,
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"))
return
},