Skip to content

Add new --validate option - #642

Open
alranel wants to merge 4 commits into
elapouya:masterfrom
alranel:validate
Open

alranel wants to merge 4 commits into
elapouya:masterfrom
alranel:validate

Conversation

@alranel

@alranel alranel commented Jun 3, 2026 •

Copy link
Copy Markdown

This PR adds a new --validate option to the CLI tool, that allows to check whether a template has valid Jinja syntax without supplying any data JSON file.
A --report FILE option is also added that can be used to write the validation report to the given file path, in JSON format.


Depends on #641 (print-error). This branch is rebased on top of print-error so the two PRs merge cleanly in order: #641 then #642.

@CAOShurong CAOShurong left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the exact head c1259ab66ec7e4ee6964a901b1486a730f1029eb from source and from a clean wheel install on Python 3.13.1. The valid-template path exits 0 and writes {"valid": true}; malformed Jinja exits 1 and writes {"valid": false, ...}; normal rendering still works. I also ran all 36 non-runner test scripts, flake8, and the sdist/wheel build successfully.

One exit-code issue blocks approval. Both newly introduced rejected invocations print an error but exit 0:

python -m docxtpl valid.docx --report report.json
Error: --report requires --validate.
EXIT=0

python -m docxtpl missing.docx --validate
Error: The specified template_path "...missing.docx" is not valid.
EXIT=0

validate_all_args() raises RuntimeError, but main() catches it and returns normally; SystemExit(1) is reached only after run_validation() starts. A CI script therefore cannot distinguish a rejected validation request from a successful validation.

Please make validation-related argument/path errors exit nonzero and add automated CLI coverage for valid syntax, invalid syntax, a missing template, and --report without --validate. No test file currently exercises the new CLI behavior.

I used OpenAI Codex to help run the exact-head regression matrix and inspect the control flow; I personally checked the commands, outputs, exit codes, and this review.

@alranel

alranel commented Oct 14, 2026

Copy link
Copy Markdown
Author

Thanks for the careful review and for catching the exit-code gap.

I've addressed both points:

  1. Nonzero exit on validation-related argument/path errors
    main() still catches RuntimeError from validate_all_args(), but when --validate or --report was requested it now calls sys.exit(1) instead of returning. That covers the two cases you reproduced (--report without --validate, and a missing/invalid template with --validate). Classical render-mode error handling is unchanged (still exits 0 after printing the error).

  2. Automated CLI coverage
    Added tests/cli_validate.py (picked up by runtests.py) which asserts exit codes for:

    • valid template + --validate → 0
    • invalid Jinja template + --validate → 1
    • missing template + --validate → 1
    • --report without --validate → 1

Happy to adjust further if anything else stands out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants