diff --git a/codefresh/helper/validation/strings.go b/codefresh/helper/validation/strings.go index 1cb5827..a54a806 100644 --- a/codefresh/helper/validation/strings.go +++ b/codefresh/helper/validation/strings.go @@ -44,6 +44,7 @@ func StringMatchesRegExp(regex string, opts ...validationopts.OptionSetter) sche SetSeverity(diag.Error). SetSummary("Invalid value"). SetDetailFormat("%s is invalid (must match %q)"). + SetRegexType(regexp2.RE2). Apply(opts) return func(v any, p cty.Path) diag.Diagnostics { @@ -53,7 +54,7 @@ func StringMatchesRegExp(regex string, opts ...validationopts.OptionSetter) sche if !re.MatchString(value) { diags = append(diags, diag.Diagnostic{ Severity: options.GetSeverity(), - Summary: fmt.Sprintf(options.GetSummary(), p), + Summary: options.GetSummary(), Detail: fmt.Sprintf(options.GetDetailFormat(), value, re.String()), }) } diff --git a/codefresh/helper/validation/validationopts/base.go b/codefresh/helper/validation/validationopts/base.go index af4bb93..cd0f128 100644 --- a/codefresh/helper/validation/validationopts/base.go +++ b/codefresh/helper/validation/validationopts/base.go @@ -10,47 +10,47 @@ type ValidationOptions struct { severity diag.Severity summary string detailFormat string - CronValidationOptions CronValidationOptions - StringValidationOptions StringValidationOptions + CronValidationOptions *CronValidationOptions + StringValidationOptions *StringValidationOptions } -type OptionSetter func(ValidationOptions) +type OptionSetter func(*ValidationOptions) func NewValidationOptions() *ValidationOptions { return &ValidationOptions{ severity: diag.Error, summary: "", detailFormat: "", - CronValidationOptions: CronValidationOptions{ + CronValidationOptions: &CronValidationOptions{ Parser: cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow), }, - StringValidationOptions: StringValidationOptions{ + StringValidationOptions: &StringValidationOptions{ regexp2.RE2, }, } } func WithSeverity(severity diag.Severity) OptionSetter { - return func(o ValidationOptions) { + return func(o *ValidationOptions) { o.SetSeverity(severity) } } func WithSummary(summary string) OptionSetter { - return func(o ValidationOptions) { + return func(o *ValidationOptions) { o.SetSummary(summary) } } func WithDetailFormat(detailFormat string) OptionSetter { - return func(o ValidationOptions) { + return func(o *ValidationOptions) { o.SetDetailFormat(detailFormat) } } func (o *ValidationOptions) Apply(setters []OptionSetter) *ValidationOptions { for _, opt := range setters { - opt(*o) + opt(o) } return o } diff --git a/codefresh/resource_pipeline_cron_trigger.go b/codefresh/resource_pipeline_cron_trigger.go index 86ca147..4c6a049 100644 --- a/codefresh/resource_pipeline_cron_trigger.go +++ b/codefresh/resource_pipeline_cron_trigger.go @@ -9,6 +9,7 @@ import ( "github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient" "github.com/codefresh-io/terraform-provider-codefresh/codefresh/helper/validation" "github.com/codefresh-io/terraform-provider-codefresh/codefresh/helper/validation/validationopts" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -51,9 +52,10 @@ func resourcePipelineCronTrigger() *schema.Resource { Type: schema.TypeString, Required: true, ValidateDiagFunc: validation.StringMatchesRegExp( - // https://github.com/codefresh-io/hermes/blob/6d75b347cb8ff471ce970a766b2285788e5e19fe/pkg/backend/dev_compose_types.json#L226 - `^[a-zA-Z0-9_+\s-#?.:]{2,128}$`, + validation.ValidCronMessageRegex, + validationopts.WithSeverity(diag.Error), validationopts.WithSummary("Invalid cron trigger message"), + validationopts.WithDetailFormat("The message %q is invalid (must match %q)."), ), }, },