Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper/schema: Force field names to be alphanum lowercase + underscores #15562

Merged
merged 1 commit into from
Jul 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion helper/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"fmt"
"os"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -661,14 +662,25 @@ func (m schemaMap) InternalValidate(topSchemaMap schemaMap) error {
if v.ValidateFunc != nil {
switch v.Type {
case TypeList, TypeSet:
return fmt.Errorf("ValidateFunc is not yet supported on lists or sets.")
return fmt.Errorf("%s: ValidateFunc is not yet supported on lists or sets.", k)
}
}

if v.Deprecated == "" && v.Removed == "" {
if !isValidFieldName(k) {
return fmt.Errorf("%s: Field name may only contain lowercase alphanumeric characters & underscores.", k)
}
}
}

return nil
}

func isValidFieldName(name string) bool {
re := regexp.MustCompile("^[a-z0-9_]+$")
return re.MatchString(name)
}

func (m schemaMap) diff(
k string,
schema *Schema,
Expand Down
42 changes: 42 additions & 0 deletions helper/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3353,6 +3353,48 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
},
true,
},

"invalid field name format #1": {
map[string]*Schema{
"with space": &Schema{
Type: TypeString,
Optional: true,
},
},
true,
},

"invalid field name format #2": {
map[string]*Schema{
"WithCapitals": &Schema{
Type: TypeString,
Optional: true,
},
},
true,
},

"invalid field name format of a Deprecated field": {
map[string]*Schema{
"WithCapitals": &Schema{
Type: TypeString,
Optional: true,
Deprecated: "Use with_underscores instead",
},
},
false,
},

"invalid field name format of a Removed field": {
map[string]*Schema{
"WithCapitals": &Schema{
Type: TypeString,
Optional: true,
Removed: "Use with_underscores instead",
},
},
false,
},
}

for tn, tc := range cases {
Expand Down