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

Fix semver validation #590

Merged
merged 3 commits into from
Sep 11, 2023
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
3 changes: 3 additions & 0 deletions .changelog/590.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
Update semver validation to allow specification of pre-release versions
```
4 changes: 2 additions & 2 deletions internal/provider/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/go-openapi/strfmt"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/go-version"
consulmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-consul-service/stable/2021-02-04/models"
vaultmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-vault-service/stable/2020-11-25/models"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -95,8 +96,7 @@ func validateStringInSlice(valid []string, ignoreCase bool) schema.SchemaValidat
// validateSemVer ensures a specified string is a SemVer.
func validateSemVer(v interface{}, path cty.Path) diag.Diagnostics {
var diagnostics diag.Diagnostics

if !regexp.MustCompile(`^v?\d+.\d+.\d+$`).MatchString(v.(string)) {
if _, err := version.NewSemver(v.(string)); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks good to me. Given that go-version is used in the provider but not for the validation I want to confirm before merging that the consul resource is okay with a user specifying a pre-release version. In other words, is there a reason why the original implementor chose only to support official release versions?

Copy link
Contributor

Choose a reason for hiding this comment

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

Disregard.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I want to confirm before merging that the consul resource is okay with a user specifying a pre-release version

Might have been a miss. But we do publicly release pre-release versions of our products (can be found here). On the Consul side, we optionally enable testing out these pre-release versions on HCP for certain customers to unblock their testing/validation processes.

msg := "must be a valid semver"
diagnostics = append(diagnostics, diag.Diagnostic{
Severity: diag.Error,
Expand Down
6 changes: 5 additions & 1 deletion internal/provider/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,12 @@ func Test_validateSemVer(t *testing.T) {
input: "1.2.3",
expected: nil,
},
"valid pre-release semver": {
input: "1.2.3-rc",
expected: nil,
},
"invalid semver": {
input: "v1.2.3.4.5",
input: "v1.2.3.beta",
expected: diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Expand Down