Title: feat(customize): add required field for input questions by bearomorphism · Pull Request #1984 · commitizen-tools/commitizen · GitHub
Open Graph Title: feat(customize): add required field for input questions by bearomorphism · Pull Request #1984 · commitizen-tools/commitizen
X Title: feat(customize): add required field for input questions by bearomorphism · Pull Request #1984 · commitizen-tools/commitizen
Description: Description Closes #1231. Adds a required field to cz_customize input questions so users can enforce non-empty answers without writing a custom Python validator. Why Users who configure commit rules entirely through pyproject.toml / .cz.json / .cz.yaml have no way to mark a free-text question as mandatory. They must either accept empty answers silently or write a Python class just to add a one-line validator. This PR provides a zero-code path for the common case. What changed File Change commitizen/question.py Added validate and required optional fields to InputQuestion TypedDict commitizen/cz/customize/customize.py questions() now shallow-copies each question dict; if type == "input" and required=True, pops required and injects a validate callable that rejects blank input docs/customization/config_file.md Added required row to the questions field table; added required = true to the TOML example tests/test_cz_customize.py Seven new unit tests covering the required validator behaviour How it works When questions() processes each question from the config: A shallow copy of the question dict is made (avoids mutating the parsed config on repeated calls). If the question has type = "input" and required = true, the required key is popped and replaced with validate = _required_validator. _required_validator returns True for non-blank strings and "This answer is required." for empty/whitespace-only input — the exact signature questionary expects. All other question types (list, confirm) pass through unchanged; required is meaningless there since both always return a value. Backward compatibility Fully backward-compatible. The required key defaults to False (absent), so existing configs are unaffected. No CLI flags, exit codes, or public APIs changed. Checklist I have read the contributing guidelines Was generative AI tooling used to co-author this PR? Yes (GitHub Copilot CLI) Code Changes Add test cases to all the changes you introduce Run uv run poe all locally to ensure this change passes linter check and tests Manually test the changes: Verify the feature/bug fix works as expected in real-world scenarios Test edge cases and error conditions Ensure backward compatibility is maintained Document any manual testing steps performed Update the documentation for the changes Expected Behavior Scenario Before After required = true on an input question KeyError / unknown key passed to questionary validate callable injected; empty input is rejected with "This answer is required." required absent / false question passes through as-is question passes through as-is (no change) list or confirm question with no required key unchanged unchanged calling questions() twice on the same config N/A second call still works correctly (no mutation) Steps to Test This Pull Request # pyproject.toml [tool.commitizen] name = "cz_customize" [tool.commitizen.customize] message_template = "{{change_type}}: {{message}}" [[tool.commitizen.customize.questions]] type = "list" name = "change_type" message = "Type" choices = [{value = "feat", name = "feat"}, {value = "fix", name = "fix"}] [[tool.commitizen.customize.questions]] type = "input" name = "message" message = "Describe the change (required):" required = true [[tool.commitizen.customize.questions]] type = "input" name = "note" message = "Optional note:" # 1. Reproduce original problem (empty answer silently accepted) git checkout v4.15.1 cz commit # pick a type, press Enter on empty message -> commit succeeds # 2. Verify fix git checkout feat/1231-customize-question-required cz commit # pick a type, press Enter on empty message -> "This answer is required." shown # type something -> commit proceeds normally Additional Context Related issue: #1231
Open Graph Description: Description Closes #1231. Adds a required field to cz_customize input questions so users can enforce non-empty answers without writing a custom Python validator. Why Users who configure commit rule...
X Description: Description Closes #1231. Adds a required field to cz_customize input questions so users can enforce non-empty answers without writing a custom Python validator. Why Users who configure commit rule...
Opengraph URL: https://github.com/commitizen-tools/commitizen/pull/1984
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:4a67588b-db3d-5e6d-849d-c4ea8dfa3358 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | E2C8:3BB6AC:BE5592:116D769:6A4E27BC |
| html-safe-nonce | 97ce5a9f4164c6041524e043d81c391694e37c1803bde27afe2e87812f44f547 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMkM4OjNCQjZBQzpCRTU1OTI6MTE2RDc2OTo2QTRFMjdCQyIsInZpc2l0b3JfaWQiOiI2MDQ2NjI0MTM1NTU5MTI5MDIwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | e53d967ec6a357bdf2ecb240176d23c1cc7951ecb01a8c225d19e52857b47255 |
| hovercard-subject-tag | pull_request:3655048409 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/commitizen-tools/commitizen/pull/1984/files |
| twitter:image | https://avatars.githubusercontent.com/u/26526132?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/26526132?s=400&v=4 |
| og:image:alt | Description Closes #1231. Adds a required field to cz_customize input questions so users can enforce non-empty answers without writing a custom Python validator. Why Users who configure commit rule... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 030096ee0db095447bfe77409d33bfac127ca7128299c58deef27c52eaa1b1f0 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/commitizen-tools/commitizen git https://github.com/commitizen-tools/commitizen.git |
| octolytics-dimension-user_id | 62252524 |
| octolytics-dimension-user_login | commitizen-tools |
| octolytics-dimension-repository_id | 106127589 |
| octolytics-dimension-repository_nwo | commitizen-tools/commitizen |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 106127589 |
| octolytics-dimension-repository_network_root_nwo | commitizen-tools/commitizen |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | d9dd20d38f8ae3c4cb6b597807431db300d0bd2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width