copier-org / copier

Library and command-line utility for rendering projects templates.
https://readthedocs.org/projects/copier/
MIT License
2.06k stars 182 forks source link

Validator and empty default value fails in copier update #1846

Closed farhaan-shamsee closed 2 weeks ago

farhaan-shamsee commented 3 weeks ago

Describe the problem

Although might be logically incorrect to provide an empty default value and a validator together.

But I have noticed that, the following combination fails during copier update.

If there is a question having empty default value and validator, after doing a copier update it gives error that validation has failed for that question. Even if the when condition is not satisfied, then also if gives the error.

This only happens in copier update. In copier copy this does not come up.

Template

Below are the 2 questions.

Even if I answer No to test_enable, the test_var gives validation error

test_enable:
  type: bool
  help: Do you want to enable test variable

test_var:
  type: str
  default: ""
  when: "[2[ test_enable ]2]"
  help: Please provide test variable value
  validator: >-
    [2% if not (test_var | regex_search('^[a-z0-9\-_]+$')) %2]
    test_var should be in lowercase. special character allowed '-_'.
    [2% endif %2]

Kindly note that my envops are different.

_envops:
  autoescape: false
  block_end_string: "%2]"
  block_start_string: "[2%"
  keep_trailing_newline: true
  variable_end_string: "]2]"
  variable_start_string: "[2["

To Reproduce

  1. Run copier update
  2. Answer No for 1st question

Logs

raise ValueError(f"Validation error for question '{var_name}': {err_msg}")
ValueError: Validation error for question 'test_var': test_var should be in lowercase. special character allowed '-_'.

Expected behavior

As the when condition is not satisfied, it should not even consider the validation.

Screenshots/screencasts/logs

image image

Operating system

Linux

Operating system distribution and version

22.04 (WSL)

Copier version

copier 9.3.1

Python version

Python 3.10.12

Installation method

pip+git

Additional context

I have not tested this thoroughly. But I have noticed this in my case. In case I am doing something wrong, please feel free to point out.

sisp commented 3 weeks ago

I think this issue is related to #1779 which was resolved via #1785. Would you mind trying the latest Copier version from the master branch?

In addition, I think a default value should always pass the validator, otherwise it's useless and the --defaults switch makes no sense.

farhaan-shamsee commented 2 weeks ago

Thanks a lot for the response. I will definitely try this out and revert here if it does not work. I will close this till then.

In addition, I think a default value should always pass the validator, otherwise it's useless and the --defaults switch makes no sense.

Exactly, that is why I mentioned that I might be logically incorrect.