fix(alertgroups): add expression validator to detect new lines on expressions (#807)
This commit is contained in:
parent
9cd402e09f
commit
f4498e85f3
5 changed files with 111 additions and 1 deletions
|
|
@ -207,6 +207,10 @@ func (a *alertGroupResource) Schema(_ context.Context, _ resource.SchemaRequest,
|
|||
Required: true,
|
||||
Validators: []validator.String{
|
||||
stringvalidator.LengthBetween(1, 600),
|
||||
// The API currently accepts expressions with trailing newlines but does not return them,
|
||||
// leading to inconsistent Terraform results. This issue has been reported to the Obs team.
|
||||
// Until it is resolved, we proactively notify users if their input contains a trailing newline.
|
||||
validate.ValidNoTrailingNewline(),
|
||||
},
|
||||
},
|
||||
"for": schema.StringAttribute{
|
||||
|
|
|
|||
|
|
@ -207,6 +207,10 @@ func (l *logAlertGroupResource) Schema(_ context.Context, _ resource.SchemaReque
|
|||
Required: true,
|
||||
Validators: []validator.String{
|
||||
stringvalidator.LengthBetween(1, 600),
|
||||
// The API currently accepts expressions with trailing newlines but does not return them,
|
||||
// leading to inconsistent Terraform results. This issue has been reported to the Obs team.
|
||||
// Until it is resolved, we proactively notify users if their input contains a trailing newline.
|
||||
validate.ValidNoTrailingNewline(),
|
||||
},
|
||||
},
|
||||
"for": schema.StringAttribute{
|
||||
|
|
|
|||
|
|
@ -997,7 +997,7 @@ func toNodepoolsPayload(ctx context.Context, m *Model, availableMachineVersions
|
|||
if v.IsNull() || v.IsUnknown() {
|
||||
continue
|
||||
}
|
||||
s, err := conversion.ToString(context.TODO(), v)
|
||||
s, err := conversion.ToString(ctx, v)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
|
|
|||
|
|
@ -313,3 +313,34 @@ func ValidDurationString() *Validator {
|
|||
},
|
||||
}
|
||||
}
|
||||
|
||||
// ValidNoTrailingNewline returns a Validator that checks if the input string has no trailing newline
|
||||
// character ("\n" or "\r\n"). If a trailing newline is present, a diagnostic error will be appended.
|
||||
func ValidNoTrailingNewline() *Validator {
|
||||
description := `The value must not have a trailing newline character ("\n" or "\r\n"). You can remove a trailing newline by using Terraform's built-in chomp() function.`
|
||||
|
||||
return &Validator{
|
||||
description: description,
|
||||
validate: func(_ context.Context, req validator.StringRequest, resp *validator.StringResponse) {
|
||||
val := req.ConfigValue.ValueString()
|
||||
if val == "" {
|
||||
return
|
||||
}
|
||||
if len(val) >= 2 && val[len(val)-2:] == "\r\n" {
|
||||
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
|
||||
req.Path,
|
||||
description,
|
||||
val,
|
||||
))
|
||||
return
|
||||
}
|
||||
if val[len(val)-1] == '\n' {
|
||||
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
|
||||
req.Path,
|
||||
description,
|
||||
val,
|
||||
))
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -845,3 +845,74 @@ func TestValidTtlDuration(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidNoTrailingNewline(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
input string
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"string with no trailing newline",
|
||||
"abc",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"string with trailing \\n",
|
||||
"abc\n",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"string with trailing \\r\\n",
|
||||
"abc\r\n",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"string with internal newlines but not trailing",
|
||||
"abc\ndef\nghi",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"empty string",
|
||||
"",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"string that is just \\n",
|
||||
"\n",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"string that is just \\r\\n",
|
||||
"\r\n",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"string with multiple newlines, trailing",
|
||||
"abc\n\n",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"string with newlines but ends with character",
|
||||
"abc\ndef\n",
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
r := validator.StringResponse{}
|
||||
va := ValidNoTrailingNewline()
|
||||
va.ValidateString(context.Background(), validator.StringRequest{
|
||||
ConfigValue: types.StringValue(tt.input),
|
||||
}, &r)
|
||||
|
||||
if !tt.isValid && !r.Diagnostics.HasError() {
|
||||
t.Fatalf("Expected validation to fail for input: %q", tt.input)
|
||||
}
|
||||
if tt.isValid && r.Diagnostics.HasError() {
|
||||
t.Fatalf("Expected validation to succeed for input: %q, but got errors: %v", tt.input, r.Diagnostics.Errors())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue