Skip to content

feat: validate message_length_limit is non-negative#1916

Open
SARAMALI15792 wants to merge 1 commit intocommitizen-tools:masterfrom
SARAMALI15792:fix/message-length-limit-validation
Open

feat: validate message_length_limit is non-negative#1916
SARAMALI15792 wants to merge 1 commit intocommitizen-tools:masterfrom
SARAMALI15792:fix/message-length-limit-validation

Conversation

@SARAMALI15792
Copy link
Copy Markdown

@SARAMALI15792 SARAMALI15792 commented Mar 31, 2026

Description

Added validation for message_length_limit to ensure it accepts only non-negative integers (>= 0). Negative values now raise InvalidCommandArgumentError instead of being silently treated as "no limit".

This PR addresses the inconsistency identified in #1909 where negative values were accepted but had unclear behavior.

Root Cause

After the fix in #1813 changed the default from None to 0 for "no limit", the validation logic still checked <= 0 instead of == 0. This meant:

  • message_length_limit = 0 → no limit ✅ (intended)
  • message_length_limit = -1 → no limit ✅ (unintended, should error)

Changes

  • Validation: Added check in Check and Commit __init__ to reject message_length_limit < 0
  • Logic fix: Changed condition from <= 0 to == 0 for "no limit" check
  • Type updates: Changed CommitArgs and CheckArgs to accept int | None for proper CLI override handling
  • Tests: Added comprehensive test coverage for negative value rejection in both commands
  • Documentation: Updated docs/config/check.md and docs/config/commit.md to clarify non-negative requirement

Why This Is Safe

  • Default is 0 (no limit), so existing configs without this setting are unaffected
  • Negative values currently work as "no limit" but this is undocumented behavior
  • Users relying on negative values can simply change to 0 (same effect, clearer intent)
  • Aligns with the v4.13.0 design where 0 means "no limit"

Expected Behavior

  • message_length_limit < 0 → raises InvalidCommandArgumentError
  • message_length_limit = 0 → disables length limit ✅
  • message_length_limit > 0 → enforces the specified limit ✅
  • CLI None value → falls back to config setting ✅

Steps to Test This Pull Request

# Test negative value raises error
echo "feat: hello" | cz check -l -1
# Expected: "message_length_limit must be a non-negative integer"

# Test zero disables limit
echo "feat: this is a very long message" | cz check -l 0
# Expected: "Commit validation: successful!"

# Test positive limit works
echo "feat: hello" | cz check -l 50
# Expected: "Commit validation: successful!"

# Run test suite
uv run poe test

Checklist

  • Add test cases to all the changes introduced
  • Run uv run poe all locally to ensure this change passes linter check and tests
  • Manually test the changes:
    • Verify negative values raise error
    • Verify zero disables limit
    • Verify positive limits work
    • Verify CLI overrides config
    • Verify config fallback works
  • Update the documentation for the changes

Related

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 31, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.99%. Comparing base (7c21c21) to head (fdcdaec).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1916   +/-   ##
=======================================
  Coverage   97.99%   97.99%           
=======================================
  Files          60       60           
  Lines        2689     2695    +6     
=======================================
+ Hits         2635     2641    +6     
  Misses         54       54           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Add validation to ensure message_length_limit is a non-negative integer (>= 0).
Negative values now raise InvalidCommandArgumentError instead of being treated
as "no limit".

Changes:
- Add validation in Check and Commit __init__ to reject negative values
- Change condition from `<= 0` to `== 0` for "no limit" check
- Update CommitArgs and CheckArgs to accept int | None for CLI override
- Add comprehensive test coverage for negative value rejection
- Update documentation to clarify non-negative requirement

Behavior:
- message_length_limit < 0: raises InvalidCommandArgumentError
- message_length_limit = 0: disables length limit (no limit)
- message_length_limit > 0: enforces the specified limit
- CLI None value: falls back to config setting

Fixes commitizen-tools#1909

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@SARAMALI15792 SARAMALI15792 force-pushed the fix/message-length-limit-validation branch from 100df54 to fdcdaec Compare March 31, 2026 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Discussion: Clarify / standardize behavior for negative message_length_limit (should it require >= 0?)

1 participant