-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathdbt_version_validator.go
More file actions
60 lines (49 loc) · 2.05 KB
/
dbt_version_validator.go
File metadata and controls
60 lines (49 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package helper
import (
"context"
"fmt"
"regexp"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)
type DbtVersionValidator struct{}
const validDbtVersionsDescription = "`major.minor.0-latest`, `major.minor.0-pre`, `compatible`, `extended`, `versionless`, `latest`, `latest-fusion`, `fusion-stable`, `fusion-extended`, `fusion-nightly` or `fusion-fallback`"
func (v DbtVersionValidator) Description(ctx context.Context) string {
return "Validates that the dbt_version is in the format " + validDbtVersionsDescription + "."
}
func (v DbtVersionValidator) MarkdownDescription(ctx context.Context) string {
return "Validates that the `dbt_version` is in the format " + validDbtVersionsDescription + "."
}
func (v DbtVersionValidator) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
// Skip validation if the value is unknown or null
if req.ConfigValue.IsUnknown() || req.ConfigValue.IsNull() {
return
}
// Get the value of dbt_version
dbtVersion := req.ConfigValue.ValueString()
// Define the regex pattern for valid dbt_version formats
validVersionPattern := `^(compatible|extended|latest|versionless|latest-fusion|fusion-stable|fusion-extended|fusion-nightly|fusion-fallback|[0-9]+\.[0-9]+\.0-(latest|pre))$`
matched, err := regexp.MatchString(validVersionPattern, dbtVersion)
if err != nil {
resp.Diagnostics.AddError(
"Regex Error",
fmt.Sprintf("An error occurred while validating the dbt_version: %s", err),
)
return
}
// If the value does not match the pattern, return an error
if !matched {
resp.Diagnostics.AddError(
"Invalid dbt_version Format",
fmt.Sprintf("The `dbt_version` must be in the format "+validDbtVersionsDescription+". Got: %s", dbtVersion),
)
}
}
// IsFusionVersion reports whether the supplied dbt_version string is one of
// the Fusion release tracks.
func IsFusionVersion(dbtVersion string) bool {
switch dbtVersion {
case "latest-fusion", "fusion-stable", "fusion-extended", "fusion-nightly", "fusion-fallback":
return true
}
return false
}