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: Allow self references with AllowOneOf, ExactlyOneOf, and RequiredWith in InternalValidate #418

Merged
merged 3 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 17 additions & 6 deletions helper/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,28 +750,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 @@ -897,7 +897,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 @@ -937,7 +939,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 @@ -948,6 +954,11 @@ func checkKeysAgainstSchemaFlags(k string, keys []string, topSchemaMap schemaMap
return fmt.Errorf("%s cannot contain Computed(When) attribute (%s)", k, key)
}
}

if requireSelfReference && !foundSelfReference {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AtLeastOneOf and ExactlyOneOf were implemented with an idempotent line that adds itself and removes duplicate, it allows the self reference to be optional. I would change this PR to have a flag for allowSelfRef and ditch the requirement for it, but rather disallow for ConflictsWith (and possibly RequiredWith)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless we remove that line in validateRequiredWith I would actually ONLY have this error for ConflictsWith, just to be consistent, I will approve either final implementation.

return fmt.Errorf("%s must reference self for AtLeastOneOf/ExactlyOneOf", k)
}

return nil
}

Expand Down
Loading