feat: add support for conditionally unsetting a question's default value #2286
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, if a question is skipped, its answer is not recorded, but its default value is available in the render context. This makes sense for computed values and conditionally skipped questions with meaningful default values, but there are use cases where a question should be skipped and its Jinja variable should be undefined (i.e., not present in the render context) because there is no meaningful default value. For example:
Here, the default value of the
database_url
question is only defined for the database engines "postgres" and "mysql" but not for "none", and we can't render, e.g., an empty string as the default value for "none" because the validator would fail. We could surround the actual validator condition with{% if database_engine != 'none' %}...{% endif %}
, but that feels hacky and redundant with thewhen
expression. What we'd actually want is to fully skip this question – I'd say by declaring the default value as unset. An unset default value of a skipped question means that there is no answer, so the validator isn't applied. Copier already behaves this way when thedefault
field is not present.To support this scenario, I've added a special Jinja variable
UNSET
which is available in the render context of thedefault
field. When this value is rendered, Copier behaves as if thedefault
field isn't present. The example from above would look like this now:Related to #2278 (comment).
WDYT, @copier-org/maintainers?