Skip to content

feat: add EmptyValuesValidator to detect empty or whitespace-only translation values#29

Merged
konradmichalik merged 1 commit intomainfrom
empty-value-validator
Jul 11, 2025
Merged

feat: add EmptyValuesValidator to detect empty or whitespace-only translation values#29
konradmichalik merged 1 commit intomainfrom
empty-value-validator

Conversation

@konradmichalik
Copy link
Copy Markdown
Contributor

@konradmichalik konradmichalik commented Jul 11, 2025

Summary by CodeRabbit

  • New Features

    • Introduced a new validator that detects empty or whitespace-only translation values in XLIFF and YAML files, issuing warnings for such cases.
  • Documentation

    • Updated documentation to include the new validator and improved the alignment of existing entries.
  • Tests

    • Added comprehensive tests for the new validator to ensure correct detection and reporting of empty translation values.
    • Updated existing tests to account for the addition of the new validator.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Jul 11, 2025

Walkthrough

A new validator, EmptyValuesValidator, was introduced to detect empty or whitespace-only translation values in XLIFF and YAML files. Documentation, command help text, and the validator registry were updated to include this validator. Comprehensive unit tests for the new validator and updated registry tests were also added.

Changes

File(s) Change Summary
README.md, src/Command/ValidateTranslationCommand.php Updated documentation and command help text to include EmptyValuesValidator.
src/Validator/EmptyValuesValidator.php Added new EmptyValuesValidator class implementing detection of empty/whitespace translation values.
src/Validator/ValidatorRegistry.php Registered EmptyValuesValidator in the list of available validators.
tests/src/Validator/EmptyValuesValidatorTest.php Added unit tests for EmptyValuesValidator, covering detection, formatting, and error handling.
tests/src/Validator/ValidatorRegistryTest.php Updated test to assert presence of EmptyValuesValidator in the registry and adjusted count.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ValidateTranslationCommand
    participant ValidatorRegistry
    participant EmptyValuesValidator
    participant Parser

    User->>ValidateTranslationCommand: Run validation command
    ValidateTranslationCommand->>ValidatorRegistry: getAvailableValidators()
    ValidatorRegistry-->>ValidateTranslationCommand: [EmptyValuesValidator, ...]
    ValidateTranslationCommand->>EmptyValuesValidator: processFile(Parser)
    EmptyValuesValidator->>Parser: getKeys()
    Parser-->>EmptyValuesValidator: keys/values
    EmptyValuesValidator-->>ValidateTranslationCommand: issues (if empty/whitespace values found)
    ValidateTranslationCommand-->>User: Display warnings/issues
Loading

Poem

A validator new has hopped in the code,
Sniffing out blanks where translations abode.
Empty or white, it gives a soft warning,
Helping our strings shine bright every morning.
With tests and docs tidy, the registry grows—
A rabbit’s delight as the translation garden glows! 🐇✨


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 0f182c3 and 94e868e.

📒 Files selected for processing (6)
  • README.md (1 hunks)
  • src/Command/ValidateTranslationCommand.php (1 hunks)
  • src/Validator/EmptyValuesValidator.php (1 hunks)
  • src/Validator/ValidatorRegistry.php (1 hunks)
  • tests/src/Validator/EmptyValuesValidatorTest.php (1 hunks)
  • tests/src/Validator/ValidatorRegistryTest.php (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (3)
src/Validator/ValidatorRegistry.php (1)
src/Validator/EmptyValuesValidator.php (1)
  • EmptyValuesValidator (12-74)
tests/src/Validator/ValidatorRegistryTest.php (1)
src/Validator/EmptyValuesValidator.php (1)
  • EmptyValuesValidator (12-74)
src/Validator/EmptyValuesValidator.php (5)
src/Parser/XliffParser.php (1)
  • XliffParser (7-81)
src/Parser/YamlParser.php (1)
  • YamlParser (9-91)
src/Result/Issue.php (2)
  • Issue (7-55)
  • getDetails (28-31)
src/Validator/AbstractValidator.php (1)
  • AbstractValidator (15-206)
src/Validator/ResultType.php (2)
  • toString (38-45)
  • toColorString (47-54)
🔇 Additional comments (13)
README.md (1)

83-84: Documentation update looks good.

The new EmptyValuesValidator is properly documented with accurate information about supported formats (XLIFF, Yaml) and warning level. The table formatting is consistent and the validator description clearly explains its purpose.

src/Command/ValidateTranslationCommand.php (1)

107-108: Help text update is consistent and accurate.

The EmptyValuesValidator is properly documented in the command help with a clear, concise description that matches its functionality. The formatting and ordering are consistent with other validators.

src/Validator/ValidatorRegistry.php (1)

18-18: Registry update is correct and consistent.

The EmptyValuesValidator is properly added to the registry in the correct alphabetical position. The class reference follows the same pattern as other validators.

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

8-8: Import statement properly added.

The EmptyValuesValidator import is correctly added to support the test assertions.


22-22: Assertion for new validator is correct.

The test properly verifies that EmptyValuesValidator is included in the registry, following the same pattern as other validators.


24-24: Count update is accurate.

The expected count is correctly updated from 4 to 5 to reflect the addition of the EmptyValuesValidator.

tests/src/Validator/EmptyValuesValidatorTest.php (1)

1-263: Excellent comprehensive test coverage.

This test file provides thorough coverage of the EmptyValuesValidator with well-structured tests that cover:

  • Core functionality: Empty strings, whitespace-only values, null values, and mixed scenarios
  • Edge cases: Invalid files, empty keys arrays, and partial emptiness
  • Integration: Supported parsers, result types, and message formatting
  • Error handling: Proper logging when files are invalid

The tests follow PHPUnit best practices with proper mocking, descriptive method names, and specific assertions. The comprehensive coverage ensures the validator will work correctly across various scenarios.

src/Validator/EmptyValuesValidator.php (6)

1-11: LGTM: Clean file structure and appropriate imports

The file header, namespace declaration, and imports follow PHP standards correctly. All necessary dependencies are imported.


12-13: LGTM: Proper class inheritance and interface implementation

The class correctly extends AbstractValidator and implements ValidatorInterface, following the established pattern for validators in this codebase.


17-40: LGTM: Robust core validation logic with proper error handling

The processFile() method implementation is well-structured:

  • Properly handles the case where extractKeys() returns null by logging an error and returning an empty array
  • Correctly identifies empty values using null === $value || '' === trim($value) which catches null values, empty strings, and whitespace-only strings
  • Preserves the original value (or converts null to empty string) for later formatting, which enables distinction between empty and whitespace-only values

The logic is sound and handles edge cases appropriately.


42-60: LGTM: Appropriate custom formatting for multiple issues

The custom formatIssueMessage() method override is necessary and well-implemented:

  • Correctly handles the array structure returned by processFile() where keys are translation keys and values are the empty/whitespace content
  • Properly distinguishes between empty and whitespace-only values for clear user feedback
  • Uses the appropriate color and level formatting consistent with the result type
  • Returns multiple formatted messages joined by newlines, which is appropriate for this validator's output

This override is necessary because the parent class expects a single 'message' key in details, but this validator needs to format multiple key-specific messages.


65-68: LGTM: Correct parser support specification

The method correctly specifies support for XliffParser and YamlParser classes, which aligns with the PR objectives to detect empty values in XLIFF and YAML files.


70-73: LGTM: Appropriate result type for empty value validation

Using ResultType::WARNING instead of the default ERROR is a thoughtful choice for empty values, as they might be intentional placeholders or work-in-progress translations rather than critical errors.

✨ Finishing Touches
  • 📝 Generate Docstrings

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 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.

@konradmichalik konradmichalik merged commit e058a50 into main Jul 11, 2025
28 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Jul 13, 2025
@konradmichalik konradmichalik deleted the empty-value-validator branch July 28, 2025 17:14
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