Skip to content

feat: add KeyCountValidator to warn about excessive translation keys#64

Merged
konradmichalik merged 1 commit intomainfrom
key-count-validator
Jul 30, 2025
Merged

feat: add KeyCountValidator to warn about excessive translation keys#64
konradmichalik merged 1 commit intomainfrom
key-count-validator

Conversation

@konradmichalik
Copy link
Copy Markdown
Contributor

@konradmichalik konradmichalik commented Jul 30, 2025

A validator to increase clarity in translation files by splitting them into multiple files when individual files are too large.

Summary by CodeRabbit

  • New Features

    • Introduced a new validator that issues warnings when translation files exceed a configurable key count threshold.
  • Documentation

    • Updated user guides and validator documentation to describe the new key count validator, including usage instructions, configuration options, and example outputs.
    • Added example files and configuration demonstrating the validator's behavior with large translation files.
  • Tests

    • Added comprehensive tests to ensure the new validator functions correctly under various scenarios.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Jul 30, 2025

Walkthrough

A new translation file validator, KeyCountValidator, was introduced to monitor the number of translation keys in files and issue warnings if a configurable threshold is exceeded. Documentation, configuration examples, fixtures, and comprehensive tests were added or updated to support and illustrate this validator's functionality and integration.

Changes

Cohort / File(s) Change Summary
Documentation Updates
README.md, docs/validators.md, tests/Fixtures/examples/README.md
Added documentation for KeyCountValidator, updated validator listings, and included new example directory in fixtures README.
Validator Implementation
src/Validator/KeyCountValidator.php
Introduced KeyCountValidator class to validate translation files against a configurable key count threshold.
Validator Registration
src/Validator/ValidatorRegistry.php
Registered KeyCountValidator in the list of available validators.
Example Fixtures
tests/Fixtures/examples/key-count/large-file.en.yaml, tests/Fixtures/examples/key-count/translation-validator.yaml
Added a large YAML translation file and corresponding configuration to demonstrate and test the validator.
Validator Tests
tests/src/Validator/KeyCountValidatorTest.php
Added comprehensive PHPUnit tests for KeyCountValidator, covering various scenarios and configuration options.
Registry Tests
tests/src/Validator/ValidatorRegistryTest.php
Updated validator registry tests to include KeyCountValidator and adjusted expectations accordingly.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ValidatorRegistry
    participant KeyCountValidator
    participant Parser
    participant Logger

    User->>ValidatorRegistry: getAvailableValidators()
    ValidatorRegistry-->>User: [ ..., KeyCountValidator, ... ]
    User->>KeyCountValidator: processFile(Parser)
    KeyCountValidator->>Parser: extractTranslationKeys()
    Parser-->>KeyCountValidator: [key1, key2, ..., keyN]
    KeyCountValidator->>KeyCountValidator: Count keys
    alt Key count > threshold
        KeyCountValidator->>Logger: log warning
        KeyCountValidator-->>User: [Warning: Key count exceeded]
    else Key count <= threshold
        KeyCountValidator-->>User: []
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~18 minutes

Poem

🐇
A validator hops in, keen and spry,
Counting translation keys as it scurries by.
If your file grows too wide,
It issues a warning with pride—
"Keep things neat, don’t let your numbers fly!"
With tests and docs, it’s ready to try.
Hooray for clean code, from this bunny ally!

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch key-count-validator

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
docs/validators.md (1)

338-345: Specify language for fenced code block.

The console output example should specify the language for better syntax highlighting and accessibility.

-```
+```console
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7973ef4 and fefee37.

📒 Files selected for processing (9)
  • README.md (1 hunks)
  • docs/validators.md (2 hunks)
  • src/Validator/KeyCountValidator.php (1 hunks)
  • src/Validator/ValidatorRegistry.php (1 hunks)
  • tests/Fixtures/examples/README.md (1 hunks)
  • tests/Fixtures/examples/key-count/large-file.en.yaml (1 hunks)
  • tests/Fixtures/examples/key-count/translation-validator.yaml (1 hunks)
  • tests/src/Validator/KeyCountValidatorTest.php (1 hunks)
  • tests/src/Validator/ValidatorRegistryTest.php (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/Validator/KeyCountValidator.php (11)
src/Config/TranslationValidatorConfig.php (4)
  • TranslationValidatorConfig (26-341)
  • hasValidatorSettings (316-319)
  • getValidatorSettings (298-301)
  • TranslationValidatorConfig (26-285)
src/Parser/JsonParser.php (1)
  • JsonParser (29-123)
src/Parser/PhpParser.php (1)
  • PhpParser (29-144)
src/Parser/XliffParser.php (1)
  • XliffParser (29-126)
src/Parser/YamlParser.php (1)
  • YamlParser (30-114)
src/Validator/AbstractValidator.php (2)
  • AbstractValidator (34-238)
  • AbstractValidator (34-223)
src/Validator/DuplicateKeysValidator.php (2)
  • DuplicateKeysValidator (33-87)
  • processFile (38-59)
src/Validator/MismatchValidator.php (2)
  • MismatchValidator (37-256)
  • processFile (42-57)
src/Validator/DuplicateValuesValidator.php (2)
  • DuplicateValuesValidator (33-120)
  • processFile (41-60)
src/Validator/EmptyValuesValidator.php (1)
  • EmptyValuesValidator (33-95)
src/Validator/ValidatorInterface.php (1)
  • ValidatorInterface (31-90)
🪛 markdownlint-cli2 (0.17.2)
docs/validators.md

338-338: Fenced code blocks should have a language specified

(MD040, fenced-code-language)

🔇 Additional comments (23)
tests/Fixtures/examples/README.md (1)

14-14: LGTM! Documentation update is consistent.

The addition of the key-count/ directory entry follows the established format and naming conventions used for other validator examples.

README.md (1)

71-71: LGTM! Validator documentation entry is well-formatted.

The new KeyCountValidator entry follows the established table format and provides an accurate, concise description of its functionality.

tests/Fixtures/examples/key-count/translation-validator.yaml (1)

1-10: LGTM! Configuration example is well-structured.

The YAML configuration properly demonstrates the KeyCountValidator settings with:

  • Clear comments explaining the demonstration purpose
  • Appropriate lower threshold (100) for testing
  • Correct YAML syntax and structure
src/Validator/ValidatorRegistry.php (1)

41-41: Validator Registration Verified

KeyCountValidator exists (src/Validator/KeyCountValidator.php) and correctly implements ValidatorInterface. Its inclusion in ValidatorRegistry follows the established pattern and requires no further changes.

tests/src/Validator/ValidatorRegistryTest.php (3)

30-30: LGTM! Import statement follows the established pattern.

The KeyCountValidator import is consistent with other validator imports in the test file.


49-49: LGTM! Test assertion properly covers the new validator.

The assertion for KeyCountValidator follows the same pattern as other validator assertions, ensuring comprehensive test coverage.


53-53: LGTM! Validator count updated correctly.

The count assertion is properly updated from 9 to 10 validators to reflect the addition of KeyCountValidator.

tests/Fixtures/examples/key-count/large-file.en.yaml (3)

1-3: Well-documented test fixture with clear purpose.

The comments clearly explain this file's role as a test fixture for the KeyCountValidator, making it easy for developers to understand its purpose.


4-334: Comprehensive and realistic translation structure.

The nested structure covers a wide range of realistic UI translation scenarios (user profiles, navigation, forms, buttons, messages, tables, etc.), making this an excellent test fixture that represents real-world usage patterns.


335-387: Effective threshold testing mechanism.

The additional_keys section with numbered keys (key_301 through key_350) provides a clean way to ensure the file exceeds the 300-key threshold for testing purposes.

docs/validators.md (2)

10-10: Proper integration into table of contents.

The KeyCountValidator is correctly positioned alphabetically in the table of contents between HtmlTagValidator and KeyNamingConventionValidator.


296-377: Comprehensive validator documentation following established patterns.

The documentation thoroughly covers the validator's purpose, configuration options, example usage, and includes helpful tips. The structure matches other validator documentation sections perfectly.

src/Validator/KeyCountValidator.php (5)

34-41: Clean class structure following established patterns.

The class properly extends AbstractValidator and implements ValidatorInterface, maintaining consistency with other validators in the codebase.


43-53: Robust configuration handling with proper validation.

The configuration method includes proper type checking and fallback to defaults, ensuring the validator works reliably even with invalid configuration values.


58-85: Well-implemented processFile method with proper error handling.

The method correctly handles both valid and invalid files, logs appropriate errors, and returns structured data that includes both human-readable messages and metadata for programmatic use.


87-90: Appropriate result type for non-critical issues.

Using ResultType::WARNING is correct for this validator since having many keys is a maintainability concern rather than a critical error.


95-98: Comprehensive parser support.

Supporting all four major parser types (Xliff, Yaml, Json, Php) ensures the validator works across different translation file formats commonly used in projects.

tests/src/Validator/KeyCountValidatorTest.php (6)

35-56: Excellent test coverage for the core functionality.

The test properly verifies that files exceeding the threshold trigger warnings with correct metadata (key count, threshold, and descriptive message).


58-90: Comprehensive boundary condition testing.

Testing both files within the threshold and files exactly at the threshold ensures the validator behaves correctly at edge cases.


92-122: Proper error handling verification.

The tests verify that invalid files are handled gracefully with appropriate logging and empty result arrays, ensuring the validator doesn't crash on malformed input.


124-230: Thorough configuration testing covering all scenarios.

The test suite covers custom thresholds, invalid configuration values, missing configuration, and empty settings, ensuring robust configuration handling in all circumstances.


232-260: Complete interface compliance verification.

Testing the validator interface methods (supportsParser, getShortName, resultTypeOnValidationFailure) ensures the validator properly integrates with the framework.


262-308: Comprehensive message formatting tests.

Testing both basic and prefixed message formatting ensures the validator outputs are properly styled and contain the expected information for CLI display.

@konradmichalik konradmichalik merged commit 9dcfc6e into main Jul 30, 2025
28 checks passed
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.

1 participant