helper/schema: validate string/bool types [GH-460]

This commit is contained in:
Mitchell Hashimoto 2014-10-19 19:56:46 -07:00
parent a44de4b96b
commit 3e1169db61
3 changed files with 34 additions and 0 deletions

View File

@ -10,6 +10,7 @@ BUG FIXES:
* core: If a resource fails to create and has provisioners, it is
marked as tainted. [GH-434]
* core: Set types are validated to be sets. [GH-413]
* core: String types are validated properly. [GH-460]
* core: Fix crash case when destroying with tainted resources. [GH-412]
* core: Don't execute provisioners in some cases on destroy.
* core: Inherited provider configurations will be properly interpolated. [GH-418]

View File

@ -823,12 +823,26 @@ func (m schemaMap) validatePrimitive(
fallthrough
case TypeList:
return m.validateList(k, raw, schema, c)
case TypeBool:
// Verify that we can parse this as the correct type
var n bool
if err := mapstructure.WeakDecode(raw, &n); err != nil {
return nil, []error{err}
}
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}
}
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}
}
default:
panic(fmt.Sprintf("Unknown validation type: %s", schema.Type))
}
return nil, nil

View File

@ -1788,6 +1788,25 @@ func TestSchemaMap_Validate(t *testing.T) {
Err: true,
},
{
Schema: map[string]*Schema{
"user_data": &Schema{
Type: TypeString,
Optional: true,
},
},
Config: map[string]interface{}{
"user_data": []interface{}{
map[string]interface{}{
"foo": "bar",
},
},
},
Err: true,
},
// Bad type, interpolated
{
Schema: map[string]*Schema{