Skip to content

Display input validation error messages. #674

@rquadling

Description

@rquadling

What problem are you facing?

If a module (root module, local ./module, or external) uses variable validation, to expose the rules to a consumer of the module, you need to duplicate the rules in the description.

It would be beneficial to also expose the potential error messages. This allows a module developer to keep the variable's description concise to what you need to know and be fully aware of what the potential errors are.

How could terraform-docs help solve your problem?

At the moment, the information that terraform-docs uses is via the terraform-config-inspect library (https://github.com/terraform-docs/terraform-config-inspect which is an unmaintained fork of https://github.com/hashicorp/terraform-config-inspect). Currently, this library does not expose the variable validation errors.

To that end, I've made a fork and pull request : Add support for presenting the error messages for input validation blocks. I've not done the comparison between https://github.com/terraform-docs/terraform-config-inspect and https://github.com/hashicorp/terraform-config-inspect to see if there's anything that needs to be added to my fork for terraform-docs to work with. I'm not an expert Go developer, but I am an active Terraform user and so learning/contributing to the projects I use daily would be "A Good Thing" (tm).

So, as an example from the tests in my fork of terraform-config-inspect

$ terraform-config-inspect tfconfig/testdata/variable-validation/

# Module `tfconfig/testdata/variable-validation/`

## Input Variables
* `variable_with_no_validation` (default `""`): This variable has no validation
* `variable_with_one_validation` (default `""`): This variable has one validation  
  Validation error messages
  * `var.variable_with_one_validation must be empty or 10 characters long.`
* `variable_with_two_validations` (required): This variable has two validations  
  Validation error messages
  * `var.variable_with_two_validations must be 10 characters long.`
  * `var.variable_with_two_validations must start with 'magic'.`

So, 0, 1, many validation rules are displayed by the CLI helper tool (.md and .json output both present).

I've then made a fork of terraform-docs and put in (currently) the bare minimum to achieve SOME output (work in progress).

So, on the same test data (snipped) ...

$ bin/darwin-amd64/terraform-docs md ../terraform-config-inspect/tfconfig/testdata/variable-validation/
## Inputs

| Name | Description | Type | Default | Required | Validation |
|------|-------------|------|---------|:--------:|------------|
| <a name="input_variable_with_no_validation"></a> [variable\_with\_no\_validation](#input\_variable\_with\_no\_validation) | This variable has no validation | `string` | `""` | no |  |
| <a name="input_variable_with_one_validation"></a> [variable\_with\_one\_validation](#input\_variable\_with\_one\_validation) | This variable has one validation | `string` | `""` | no | ***VALIDATIONS HERE*** |
| <a name="input_variable_with_two_validations"></a> [variable\_with\_two\_validations](#input\_variable\_with\_two\_validations) | This variable has two validations | `string` | n/a | yes | ***VALIDATIONS HERE******VALIDATIONS HERE*** |

As you can see, the placeholder in the template just to display ***VALIDATIONS HERE*** for each validation error message is working. This is still a work in progress.

So.

What I'm asking is is this useful to the community? If so, I'm happy to do the work, but would like some guidance on the presentation of the validation messages. Unfortunately, the markdown isn't very pretty to read in table mode, a lack of multi-line support for markdown tables (at least the rendering engines I use don't seem to have any useful extensions in play), so I think just putting a <br> between each entry is the only option.

The possibility of a Validation section may be a better idea.

Hopefully this all makes sense.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy