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

Incorrect diagnostics location for ValidateDiagFunc #1175

Open
wzagrajcz opened this issue Mar 17, 2023 · 0 comments
Open

Incorrect diagnostics location for ValidateDiagFunc #1175

wzagrajcz opened this issue Mar 17, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@wzagrajcz
Copy link

SDK version

v2.25.0

Relevant provider source code

"aaa": {
	Optional:    true,
	Type:        schema.TypeList,
			MaxItems:    1,
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"start_date": {
						ValidateDiagFunc: validateRegex("^[1-9]\\d*$"),
						Optional:         true,
						Type:             schema.TypeString,
					},
					"end_date": {
						ValidateDiagFunc: validateRegex("^[1-9]\\d*$"),
						Optional:         true,
						Type:             schema.TypeString,
					},
					// some other fields (let's call them "abc", "def")
				},
			},
		},

Terraform Configuration Files

      aaa {
        end_date   = "test1"
        start_date  = "test2"
        abc = "abc_value"
        def = "def_value"
      }

Values for start and end_date does not pass the validation.

Debug Output

Expected Behavior


│ Error: value start_date: "test2" does not match the pattern "^[1-9]\d*$"

│ with data.datasourcename.name,
│ on filename.tf line 132, in data "datasourcename" "name":
│ 132: start_date = "test2"



│ Error: value end_date: "test1" does not match the pattern "^[1-9]\d*$"

│ with data.datasourcename.name,
│ on filename.tf line 133, in data "datasourcename" "name":
│ 133: end_date = "test1"

Both validation fails and the location of failed places is correct. It's especially useful with big and flat schemas.

Actual Behavior


│ Error: value start_date: "test2" does not match the pattern "^[1-9]\d*$"

│ with data.datasourcename.name,
│ on filename.tf line 131, in data "datasourcename" "name":
│ 131: abc = "abc_value"



│ Error: value end_date: "test1" does not match the pattern "^[1-9]\d*$"

│ with data.datasourcename.name,
│ on filename.tf line 131, in data "datasourcename" "name":
│ 131: abc = "abc_value"

The location where are the issues are incorrect: it's pointing that it failed on "abc" and "abc" (my duplication here was intended) while it should point to "start_date" and "end_date" where both validations failed. Please note that when trying the terraform plan several times, the incorrect lines reported by diagnostics changes (the bigger the schema is, the more it changes).

Steps to Reproduce

  1. terraform init
  2. terraform plan (often can be tried several times depending on the schema size)

Preanalysis ?

I did brief debugging on that topic and it seems that the root cause is in the validateObject function of the helper/schema/schema.go file. As a parameter it receives path cty.Path (where Path is a slice). As You know slice is actually a view over an array (it'll be needed later). Later (when there is iteration over schema), that path is used to build new path for validate function. If the validate returns something notempty, the it's saved into the diags.
And here where the root cause imo occurs: on next iteration it appends new schema element to path. As a result it overwrites the path value that was already stored into the diags as it's based on the same array. That causes incorrect path and duplication as it'll always show the last element of iterating over schema.
So probably copying path slice in the-over-schema loop and using that fresh copy every time should fix the issue.

Regardless if You find results of my debugging useful it'd be great if You could please take care of that issue.

Best regards,
Wojciech

@wzagrajcz wzagrajcz added the bug Something isn't working label Mar 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant