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

Validator and empty default value fails in copier update #1846

Closed
farhaan-shamsee opened this issue Nov 3, 2024 · 2 comments
Closed

Validator and empty default value fails in copier update #1846

farhaan-shamsee opened this issue Nov 3, 2024 · 2 comments
Labels
bug triage Trying to make sure if this is valid or not

Comments

@farhaan-shamsee
Copy link

farhaan-shamsee commented Nov 3, 2024

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.

@farhaan-shamsee farhaan-shamsee added bug triage Trying to make sure if this is valid or not labels Nov 3, 2024
@sisp
Copy link
Member

sisp commented Nov 3, 2024

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug triage Trying to make sure if this is valid or not
Projects
None yet
Development

No branches or pull requests

2 participants