Skip to content

Introduce Data Source, Provider, and Resource Level Validators#60

Merged
bflad merged 3 commits intomainfrom
bflad/schema-validators
Aug 30, 2022
Merged

Introduce Data Source, Provider, and Resource Level Validators#60
bflad merged 3 commits intomainfrom
bflad/schema-validators

Conversation

@bflad
Copy link
Copy Markdown
Contributor

@bflad bflad commented Aug 28, 2022

Closes #48

These configuration validators are intended for usage outside the schema definition, which may be easier to comprehend or introduce with code generation.

For example:

func (r exampleResource) ConfigValidators(ctx context.Context) []resource.ConfigValidator {
  return []resource.ConfigValidator{
    // Schema defined attributes named attr1 and attr2 must have one or more configured
    resourcevalidator.AtLeastOneOf(
      path.MatchRoot("attr1"),
      path.MatchRoot("attr2"),
    ),
    // Schema defined attributes named attr3 and attr4 cannot be configured together
    resourcevalidator.Conflicting(
      path.MatchRoot("attr3"),
      path.MatchRoot("attr4"),
    ),
    // Schema defined attributes named attr5 and attr6 must only have one configured
    resourcevalidator.ExactlyOneOf(
      path.MatchRoot("attr5"),
      path.MatchRoot("attr6"),
    ),
    // Schema defined attributes named attr7 and attr8 must be configured together
    resourcevalidator.RequiredTogether(
      path.MatchRoot("attr7"),
      path.MatchRoot("attr8"),
    ),
  }
}

Reference: #48

These configuration validators are intended for usage outside the schema definition, which may be easier to comprehend or introduce with code generation.

For example:

```go
func (r exampleResource) ConfigValidators(ctx context.Context) []resource.ConfigValidator {
  return []resource.ConfigValidator{
    // Schema defined attributes named attr1 and attr2 must have one or more configured
    resourcevalidator.AtLeastOneOf(
      path.MatchRoot("attr1"),
      path.MatchRoot("attr2"),
    ),
    // Schema defined attributes named attr3 and attr4 cannot be configured together
    resourcevalidator.Conflicting(
      path.MatchRoot("attr3"),
      path.MatchRoot("attr4"),
    ),
    // Schema defined attributes named attr5 and attr6 must only have one configured
    resourcevalidator.ExactlyOneOf(
      path.MatchRoot("attr5"),
      path.MatchRoot("attr6"),
    ),
    // Schema defined attributes named attr7 and attr8 must be configured together
    resourcevalidator.RequiredTogether(
      path.MatchRoot("attr7"),
      path.MatchRoot("attr8"),
    ),
  }
}
```
@bflad bflad added the enhancement New feature or request label Aug 28, 2022
@bflad bflad added this to the v0.5.0 milestone Aug 28, 2022
@bflad bflad requested a review from a team as a code owner August 28, 2022 18:50
Copy link
Copy Markdown
Contributor

@bendbennett bendbennett left a comment

Choose a reason for hiding this comment

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

LGTM

@bflad bflad merged commit 7425c06 into main Aug 30, 2022
@bflad bflad deleted the bflad/schema-validators branch August 30, 2022 14:42
@github-actions
Copy link
Copy Markdown

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Sep 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Resource/Data-Source/Provider level Schema Configuration Validators

2 participants