From 3e1169db619cd6a3144d6aafacccc3706723bcb7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 19 Oct 2014 19:56:46 -0700 Subject: [PATCH] helper/schema: validate string/bool types [GH-460] --- CHANGELOG.md | 1 + helper/schema/schema.go | 14 ++++++++++++++ helper/schema/schema_test.go | 19 +++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33861d704..24ffa7855 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/helper/schema/schema.go b/helper/schema/schema.go index 6b0afa6b2..c0fb99210 100644 --- a/helper/schema/schema.go +++ b/helper/schema/schema.go @@ -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 diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index ea71351ef..e966eb34b 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -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{