Skip to content

Improve phone number validator type validation - #3721

Open
4arjun wants to merge 2 commits into
ohcnetwork:developfrom
4arjun:develop
Open

Improve phone number validator type validation#3721
4arjun wants to merge 2 commits into
ohcnetwork:developfrom
4arjun:develop

Conversation

@4arjun

@4arjun 4arjun commented Jul 31, 2026

Copy link
Copy Markdown

Proposed Changes

  • Added explicit validation for unsupported PhoneNumberValidator types.
  • Changed unsupported validator type handling from raw KeyError/TypeError failures to clear ValueError messages.
  • Require types to be a non-empty collection so one-shot iterators/generators are rejected for safer Django validator deconstruction.
  • Preserved existing behavior for supported phone number types.
  • Added tests for invalid types, unsupported types, non-string values, unhashable values, and iterator input.

Associated Issue

  • No linked issue. This is a small validation hardening change for shared phone number validator behavior.

Merge Checklist

  • Tests added/fixed
  • Update docs in /docs
  • Linting Complete
  • Verified no migration changes are generated

Only PR's with test cases included and passing lint and test pipelines will be reviewed

@ohcnetwork/care-backend-maintainers @ohcnetwork/care-backend-admins

Summary by CodeRabbit

  • Bug Fixes

    • Improved phone-number validation configuration handling.
    • Empty, unsupported, unhashable, or non-string phone-number types now produce clear validation errors.
    • Phone-number type configurations must now use a non-string collection, with supported mobile and landline types validated correctly.
  • Tests

    • Added coverage for invalid, empty, unsupported, unhashable, and non-string configuration inputs.
    • Added validation coverage for supported mobile and landline types.

@4arjun
4arjun requested a review from a team as a code owner July 31, 2026 12:39
Copilot AI review requested due to automatic review settings July 31, 2026 12:39
@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Changes

Phone number validation

Layer / File(s) Summary
Validate phone-number type inputs
care/utils/models/validators.py
PhoneNumberValidator now requires a non-string Collection[str], stores it as a tuple, rejects empty collections, and reports unsupported or non-string types.
Test validation behavior
care/utils/tests/test_phone_number_validator.py
Tests cover empty, unsupported, unhashable, non-string, mobile, and landline type inputs.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: vigneshhari, copilot

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly and concisely describes the main change to phone number validator type validation.
Description check ✅ Passed The description covers the proposed changes, associated issue context, tests, linting, and merge checklist.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@care/utils/models/validators.py`:
- Around line 116-119: The validator constructor’s unsupported-type handling
must consistently raise ValueError for non-string elements, including unhashable
values such as [] and hashable values such as 1. Update the validation flow
around unsupported_types and self.regex_map to validate each element’s type
before lookup, and format invalid values safely without str.join type errors.
Add regression tests covering both cases.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 7634c81d-b69c-4f34-8657-4a9ad4befe95

📥 Commits

Reviewing files that changed from the base of the PR and between 6eb0df0 and 571fcf3.

📒 Files selected for processing (2)
  • care/utils/models/validators.py
  • care/utils/tests/test_phone_number_validator.py

Comment thread care/utils/models/validators.py Outdated

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

Pull request overview

This PR hardens PhoneNumberValidator initialization in care.utils.models.validators by validating the types argument more explicitly, replacing an implicit KeyError failure mode with clearer ValueErrors and adding regression tests for invalid inputs.

Changes:

  • Added explicit checks for empty types and unsupported phone number types, raising ValueError with clearer messages.
  • Added unit tests for invalid types values and unsupported type names.
  • Added a new test asserting generator input is accepted for types.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
care/utils/models/validators.py Adds explicit types validation and clearer error handling for unsupported types.
care/utils/tests/test_phone_number_validator.py Adds tests covering invalid types, unsupported types, and generator input.

Comment thread care/utils/models/validators.py Outdated
Comment thread care/utils/models/validators.py Outdated
Comment thread care/utils/tests/test_phone_number_validator.py Outdated
Copilot AI review requested due to automatic review settings July 31, 2026 12:49

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Suppressed comments (2)

care/utils/models/validators.py:114

  • types is validated as a Collection, which rejects generators/iterators. The PR description says generator inputs are accepted; if that is intended, the runtime check should allow general iterables and normalize via tuple(types) (catching TypeError) before emptiness/unsupported-type checks.
    def __init__(self, types: Collection[str], *args, **kwargs):
        if not isinstance(types, Collection) or isinstance(types, str):
            msg = "The `types` argument must be a non-empty collection."
            raise ValueError(msg)

        types = tuple(types)
        if not types:
            msg = "The `types` argument must be a non-empty collection."
            raise ValueError(msg)

care/utils/tests/test_phone_number_validator.py:142

  • This test currently treats a generator of types as invalid, which contradicts the PR description claiming generator inputs are accepted. If generator support is intended, remove it from the invalid list and add a positive test that a generator-based types works end-to-end.
    def test_types_must_be_non_empty_collection(self):
        invalid_types = ["mobile", (), (type_ for type_ in ("mobile",))]

        for types in invalid_types:
            with self.assertRaisesMessage(
                ValueError,
                "The `types` argument must be a non-empty collection.",
            ):
                PhoneNumberValidator(types=types)

@eeshsaxena eeshsaxena left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice tightening. Two real improvements here:

  • Materializing types = tuple(types) before the emptiness check avoids the old len(types) blowing up on a generator (generators have no __len__), and requiring Collection rather than Iterable makes the empty check well-defined.
  • The unsupported-type loop is safe against unhashable inputs: not isinstance(type_, str) or type_ not in self.regex_map short-circuits on the isinstance check, so a [] never reaches [] in self.regex_map, which would otherwise raise TypeError: unhashable type. The [] and 1 test cases exercise that path.

Reads correct.

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.

3 participants