Merge pull request #2265 from hashicorp/f-schema-validate-field

Support arbitrary per-field schema validation
This commit is contained in:
Paul Hinze 2015-06-22 18:01:54 -05:00
commit aa8cf572a8
3 changed files with 128 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"regexp"
"strings"
"time"
@ -153,6 +154,20 @@ func resourceAwsDbInstance() *schema.Resource {
"final_snapshot_identifier": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: func(v interface{}) (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"))
}
if regexp.MustCompile(`--`).MatchString(fsi) {
es = append(es, fmt.Errorf("cannot contain two consecutive hyphens"))
}
if regexp.MustCompile(`-$`).MatchString(fsi) {
es = append(es, fmt.Errorf("cannot end in a hyphen"))
}
return
},
},
"db_subnet_group_name": &schema.Schema{

View File

@ -131,6 +131,14 @@ type Schema struct {
// This string is the message shown to the user with instructions on
// what do to about the removed attribute.
Removed string
// ValidateFunc allows individual fields to define arbitrary validation
// logic. It is yielded the provided config value as an interface{} that is
// guaranteed to be of the proper Schema type, and it can yield warnings or
// errors based on inspection of that value.
//
// ValidateFunc currently only works for primitive types.
ValidateFunc SchemaValidateFunc
}
// SchemaDefaultFunc is a function called to return a default value for
@ -173,6 +181,10 @@ type SchemaSetFunc func(interface{}) int
// to be stored in the state.
type SchemaStateFunc func(interface{}) string
// SchemaValidateFunc is a function used to validate a single field in the
// schema.
type SchemaValidateFunc func(interface{}) ([]string, []error)
func (s *Schema) GoString() string {
return fmt.Sprintf("*%#v", *s)
}
@ -503,6 +515,13 @@ func (m schemaMap) InternalValidate(topSchemaMap schemaMap) error {
}
}
}
if v.ValidateFunc != nil {
switch v.Type {
case TypeList, TypeSet, TypeMap:
return fmt.Errorf("ValidateFunc is only supported on primitives.")
}
}
}
return nil
@ -1118,6 +1137,7 @@ func (m schemaMap) validatePrimitive(
return nil, nil
}
var decoded interface{}
switch schema.Type {
case TypeBool:
// Verify that we can parse this as the correct type
@ -1125,28 +1145,36 @@ func (m schemaMap) validatePrimitive(
if err := mapstructure.WeakDecode(raw, &n); err != nil {
return nil, []error{err}
}
decoded = n
case TypeInt:
// Verify that we can parse this as an int
var n int
if err := mapstructure.WeakDecode(raw, &n); err != nil {
return nil, []error{err}
}
decoded = n
case TypeFloat:
// Verify that we can parse this as an int
var n float64
if err := mapstructure.WeakDecode(raw, &n); err != nil {
return nil, []error{err}
}
decoded = n
case TypeString:
// Verify that we can parse this as a string
var n string
if err := mapstructure.WeakDecode(raw, &n); err != nil {
return nil, []error{err}
}
decoded = n
default:
panic(fmt.Sprintf("Unknown validation type: %#v", schema.Type))
}
if schema.ValidateFunc != nil {
return schema.ValidateFunc(decoded)
}
return nil, nil
}

View File

@ -2796,6 +2796,20 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
},
false,
},
// ValidateFunc on non-primitive
{
map[string]*Schema{
"foo": &Schema{
Type: TypeMap,
Required: true,
ValidateFunc: func(v interface{}) (ws []string, es []error) {
return
},
},
},
true,
},
}
for i, tc := range cases {
@ -3399,7 +3413,77 @@ func TestSchemaMap_Validate(t *testing.T) {
Err: true,
Errors: []error{
fmt.Errorf("\"optional_att\": conflicts with required_att (\"required-val\")"),
fmt.Errorf(`"optional_att": conflicts with required_att ("required-val")`),
},
},
"Good with ValidateFunc": {
Schema: map[string]*Schema{
"validate_me": &Schema{
Type: TypeString,
Required: true,
ValidateFunc: func(v interface{}) (ws []string, es []error) {
return
},
},
},
Config: map[string]interface{}{
"validate_me": "valid",
},
Err: false,
},
"Bad with ValidateFunc": {
Schema: map[string]*Schema{
"validate_me": &Schema{
Type: TypeString,
Required: true,
ValidateFunc: func(v interface{}) (ws []string, es []error) {
es = append(es, fmt.Errorf("something is not right here"))
return
},
},
},
Config: map[string]interface{}{
"validate_me": "invalid",
},
Err: true,
Errors: []error{
fmt.Errorf(`something is not right here`),
},
},
"ValidateFunc not called when type does not match": {
Schema: map[string]*Schema{
"number": &Schema{
Type: TypeInt,
Required: true,
ValidateFunc: func(v interface{}) (ws []string, es []error) {
t.Fatalf("Should not have gotten validate call")
return
},
},
},
Config: map[string]interface{}{
"number": "NaN",
},
Err: true,
},
"ValidateFunc gets decoded type": {
Schema: map[string]*Schema{
"maybe": &Schema{
Type: TypeBool,
Required: true,
ValidateFunc: func(v interface{}) (ws []string, es []error) {
if _, ok := v.(bool); !ok {
t.Fatalf("Expected bool, got: %#v", v)
}
return
},
},
},
Config: map[string]interface{}{
"maybe": "true",
},
},
}