Skip to content

Commit

Permalink
helper/schema: Allow and require self references with AllowOneOf and …
Browse files Browse the repository at this point in the history
…ExactlyOneOf in InternalValidate

Reference: hashicorp#407
Reference: hashicorp#416
  • Loading branch information
bflad committed Apr 30, 2020
1 parent ac71e59 commit ddd2b16
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
23 changes: 17 additions & 6 deletions helper/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,28 +770,28 @@ func (m schemaMap) internalValidate(topSchemaMap schemaMap, attrsOnly bool) erro
}

if len(v.ConflictsWith) > 0 {
err := checkKeysAgainstSchemaFlags(k, v.ConflictsWith, topSchemaMap, v)
err := checkKeysAgainstSchemaFlags(k, v.ConflictsWith, topSchemaMap, v, false)
if err != nil {
return fmt.Errorf("ConflictsWith: %+v", err)
}
}

if len(v.RequiredWith) > 0 {
err := checkKeysAgainstSchemaFlags(k, v.RequiredWith, topSchemaMap, v)
err := checkKeysAgainstSchemaFlags(k, v.RequiredWith, topSchemaMap, v, false)
if err != nil {
return fmt.Errorf("RequiredWith: %+v", err)
}
}

if len(v.ExactlyOneOf) > 0 {
err := checkKeysAgainstSchemaFlags(k, v.ExactlyOneOf, topSchemaMap, v)
err := checkKeysAgainstSchemaFlags(k, v.ExactlyOneOf, topSchemaMap, v, true)
if err != nil {
return fmt.Errorf("ExactlyOneOf: %+v", err)
}
}

if len(v.AtLeastOneOf) > 0 {
err := checkKeysAgainstSchemaFlags(k, v.AtLeastOneOf, topSchemaMap, v)
err := checkKeysAgainstSchemaFlags(k, v.AtLeastOneOf, topSchemaMap, v, true)
if err != nil {
return fmt.Errorf("AtLeastOneOf: %+v", err)
}
Expand Down Expand Up @@ -860,7 +860,9 @@ func (m schemaMap) internalValidate(topSchemaMap schemaMap, attrsOnly bool) erro
return nil
}

func checkKeysAgainstSchemaFlags(k string, keys []string, topSchemaMap schemaMap, self *Schema) error {
func checkKeysAgainstSchemaFlags(k string, keys []string, topSchemaMap schemaMap, self *Schema, requireSelfReference bool) error {
var foundSelfReference bool

for _, key := range keys {
parts := strings.Split(key, ".")
sm := topSchemaMap
Expand Down Expand Up @@ -900,7 +902,11 @@ func checkKeysAgainstSchemaFlags(k string, keys []string, topSchemaMap schemaMap
}

if target == self {
return fmt.Errorf("%s cannot reference self (%s)", k, key)
if !requireSelfReference {
return fmt.Errorf("%s cannot reference self (%s)", k, key)
}

foundSelfReference = true
}

if target.Required {
Expand All @@ -911,6 +917,11 @@ func checkKeysAgainstSchemaFlags(k string, keys []string, topSchemaMap schemaMap
return fmt.Errorf("%s cannot contain Computed(When) attribute (%s)", k, key)
}
}

if requireSelfReference && !foundSelfReference {
return fmt.Errorf("%s must reference self for AtLeastOneOf/ExactlyOneOf", k)
}

return nil
}

Expand Down
18 changes: 10 additions & 8 deletions helper/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3600,6 +3600,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
"config_block_attr": {
Type: TypeList,
Optional: true,
MaxItems: 1,
Elem: &Resource{
Schema: map[string]*Schema{
"nested_attr": {
Expand All @@ -3611,7 +3612,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
},
},
},
true, // TODO: AtLeastOneOf self reference is necessary
false,
},

"AtLeastOneOf list index syntax with list configuration block existing attribute": {
Expand All @@ -3635,7 +3636,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
AtLeastOneOf: []string{"config_block_attr.0.nested_attr"},
},
},
false, // TODO: AtLeastOneOf self reference is necessary
true,
},

"AtLeastOneOf list index syntax with list configuration block missing attribute": {
Expand Down Expand Up @@ -3843,7 +3844,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
AtLeastOneOf: []string{"map_attr"},
},
},
false, // TODO: AtLeastOneOf self reference is necessary
true,
},

"AtLeastOneOf string syntax with self reference": {
Expand All @@ -3854,7 +3855,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
AtLeastOneOf: []string{"test"},
},
},
true, // TODO: AtLeastOneOf self reference is necessary
false,
},

"ConflictsWith list index syntax with self reference": {
Expand Down Expand Up @@ -4124,6 +4125,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
"config_block_attr": {
Type: TypeList,
Optional: true,
MaxItems: 1,
Elem: &Resource{
Schema: map[string]*Schema{
"nested_attr": {
Expand All @@ -4135,7 +4137,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
},
},
},
true, // TODO: ExactlyOneOf self reference is necessary
false,
},

"ExactlyOneOf list index syntax with list configuration block existing attribute": {
Expand All @@ -4159,7 +4161,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
ExactlyOneOf: []string{"config_block_attr.0.nested_attr"},
},
},
false, // TODO: ExactlyOneOf self reference is necessary
true,
},

"ExactlyOneOf list index syntax with list configuration block missing attribute": {
Expand Down Expand Up @@ -4367,7 +4369,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
ExactlyOneOf: []string{"map_attr"},
},
},
false, // TODO: ExactlyOneOf self reference is necessary
true,
},

"ExactlyOneOf string syntax with self reference": {
Expand All @@ -4378,7 +4380,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
AtLeastOneOf: []string{"test"},
},
},
true, // TODO: ExactlyOneOf self reference is necessary
false,
},

"Sub-resource invalid": {
Expand Down

0 comments on commit ddd2b16

Please sign in to comment.