Merge pull request #2265 from hashicorp/f-schema-validate-field
Support arbitrary per-field schema validation
This commit is contained in:
commit
aa8cf572a8
|
@ -3,6 +3,7 @@ package aws
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -153,6 +154,20 @@ 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) {
|
||||||
|
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{
|
"db_subnet_group_name": &schema.Schema{
|
||||||
|
|
|
@ -131,6 +131,14 @@ type Schema struct {
|
||||||
// This string is the message shown to the user with instructions on
|
// This string is the message shown to the user with instructions on
|
||||||
// what do to about the removed attribute.
|
// what do to about the removed attribute.
|
||||||
Removed string
|
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
|
// 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.
|
// to be stored in the state.
|
||||||
type SchemaStateFunc func(interface{}) string
|
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 {
|
func (s *Schema) GoString() string {
|
||||||
return fmt.Sprintf("*%#v", *s)
|
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
|
return nil
|
||||||
|
@ -1118,6 +1137,7 @@ func (m schemaMap) validatePrimitive(
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var decoded interface{}
|
||||||
switch schema.Type {
|
switch schema.Type {
|
||||||
case TypeBool:
|
case TypeBool:
|
||||||
// Verify that we can parse this as the correct type
|
// 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 {
|
if err := mapstructure.WeakDecode(raw, &n); err != nil {
|
||||||
return nil, []error{err}
|
return nil, []error{err}
|
||||||
}
|
}
|
||||||
|
decoded = n
|
||||||
case TypeInt:
|
case TypeInt:
|
||||||
// Verify that we can parse this as an int
|
// Verify that we can parse this as an int
|
||||||
var n int
|
var n int
|
||||||
if err := mapstructure.WeakDecode(raw, &n); err != nil {
|
if err := mapstructure.WeakDecode(raw, &n); err != nil {
|
||||||
return nil, []error{err}
|
return nil, []error{err}
|
||||||
}
|
}
|
||||||
|
decoded = n
|
||||||
case TypeFloat:
|
case TypeFloat:
|
||||||
// Verify that we can parse this as an int
|
// Verify that we can parse this as an int
|
||||||
var n float64
|
var n float64
|
||||||
if err := mapstructure.WeakDecode(raw, &n); err != nil {
|
if err := mapstructure.WeakDecode(raw, &n); err != nil {
|
||||||
return nil, []error{err}
|
return nil, []error{err}
|
||||||
}
|
}
|
||||||
|
decoded = n
|
||||||
case TypeString:
|
case TypeString:
|
||||||
// Verify that we can parse this as a string
|
// Verify that we can parse this as a string
|
||||||
var n string
|
var n string
|
||||||
if err := mapstructure.WeakDecode(raw, &n); err != nil {
|
if err := mapstructure.WeakDecode(raw, &n); err != nil {
|
||||||
return nil, []error{err}
|
return nil, []error{err}
|
||||||
}
|
}
|
||||||
|
decoded = n
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("Unknown validation type: %#v", schema.Type))
|
panic(fmt.Sprintf("Unknown validation type: %#v", schema.Type))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if schema.ValidateFunc != nil {
|
||||||
|
return schema.ValidateFunc(decoded)
|
||||||
|
}
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2796,6 +2796,20 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
|
||||||
},
|
},
|
||||||
false,
|
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 {
|
for i, tc := range cases {
|
||||||
|
@ -3399,7 +3413,77 @@ func TestSchemaMap_Validate(t *testing.T) {
|
||||||
|
|
||||||
Err: true,
|
Err: true,
|
||||||
Errors: []error{
|
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",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue