Skip to content

[ENG-796] fix:added age range validation for monetary component - #3718

Open
nandkishorr wants to merge 2 commits into
developfrom
ENG-796-fix-issue-with-age-in-charge-item-create-conditions
Open

[ENG-796] fix:added age range validation for monetary component#3718
nandkishorr wants to merge 2 commits into
developfrom
ENG-796-fix-issue-with-age-in-charge-item-create-conditions

Conversation

@nandkishorr

@nandkishorr nandkishorr commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Proposed Changes

  • Added validation for conditions in PatientAgeMetric.

Associated Issue

Merge Checklist

  • Tests added/fixed
  • Update docs in /docs
  • Linting Complete
  • Any other necessary step

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 validation for patient-age evaluation rules with stricter schemas for exact ages and in-range values.
    • Enforced correct range constraints to ensure minimum values don’t exceed maximum values.
    • Standardized and clarified supported age units (years, months, days) to ensure consistent comparisons and conversions.

@nandkishorr nandkishorr self-assigned this Jul 27, 2026
@nandkishorr
nandkishorr requested a review from a team as a code owner July 27, 2026 08:06
Copilot AI review requested due to automatic review settings July 27, 2026 08:06
@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Changes

Patient age validation

Layer / File(s) Summary
Rule schemas and operation-specific validation
care/utils/evaluators/evaluation_metric/patient_age.py
Adds typed Pydantic schemas for equality and range rules, enforces ordered ranges, and validates payloads according to the operation.
Enum-based unit conversion
care/utils/evaluators/evaluation_metric/patient_age.py
Uses ValueType enum members when selecting patient-age unit conversions.

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

Suggested reviewers: copilot, vigneshhari

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title matches the main change by calling out age-range validation, though the wording is a bit awkward.
Description check ✅ Passed The PR description follows the template with Proposed Changes, Associated Issue, and Merge Checklist filled in.
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.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ENG-796-fix-issue-with-age-in-charge-item-create-conditions

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.

@greptile-apps

greptile-apps Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR strengthens patient-age condition validation.

  • Adds strict schemas for exact-age and age-range rules.
  • Rejects ranges whose minimum exceeds their maximum.
  • Restricts age units to years, months, or days and defaults omitted units to years.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
care/utils/evaluators/evaluation_metric/patient_age.py Adds validation that resolves the previously reported inverted-range and unsupported-unit issues without exposing a distinct related failure.

Reviews (2): Last reviewed commit: "fix:added review changes" | Re-trigger Greptile

Comment thread care/utils/evaluators/evaluation_metric/patient_age.py Outdated
Comment thread care/utils/evaluators/evaluation_metric/patient_age.py Outdated

@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/evaluators/evaluation_metric/patient_age.py`:
- Around line 14-24: Update ValueSpec and RangeSpec to constrain value_type to
the supported units years, months, and days, then add a Pydantic 2 model
validator to RangeSpec that rejects configurations where min exceeds max. Add
validation tests covering unsupported units and inverted ranges.
🪄 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: 0aec033b-038c-4825-a935-c9b49fa850ce

📥 Commits

Reviewing files that changed from the base of the PR and between 6eb0df0 and 5e197e7.

📒 Files selected for processing (1)
  • care/utils/evaluators/evaluation_metric/patient_age.py

Comment thread care/utils/evaluators/evaluation_metric/patient_age.py

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 adds rule-level validation for the patient_age evaluation metric so that condition payloads used in qualified ranges / monetary components are checked earlier during spec validation.

Changes:

  • Introduces Pydantic schemas for patient_age rule payloads.
  • Adds PatientAgeMetric.validate_rule() to validate equality and in_range condition structures.
Comments suppressed due to low confidence (1)

care/utils/evaluators/evaluation_metric/patient_age.py:40

  • New rule validation paths (equality and in_range) aren’t covered by tests. Adding API-level tests for invalid patient_age condition payloads (e.g., missing keys, min > max, invalid value_type) would help prevent regressions in monetary component / qualified range validation.
    @classmethod
    def validate_rule(cls, operation, value):
        super().validate_rule(operation, value)
        if operation == AllowedOperations.equality.value:
            ValueSpec.model_validate(value)
        elif operation == AllowedOperations.in_range.value:
            RangeSpec.model_validate(value)

Comment thread care/utils/evaluators/evaluation_metric/patient_age.py
@codecov

codecov Bot commented Jul 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 60.71429% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.44%. Comparing base (6eb0df0) to head (a1b0d09).

Files with missing lines Patch % Lines
.../utils/evaluators/evaluation_metric/patient_age.py 60.71% 10 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #3718      +/-   ##
===========================================
- Coverage    79.45%   79.44%   -0.02%     
===========================================
  Files          480      480              
  Lines        23215    23240      +25     
  Branches      2420     2423       +3     
===========================================
+ Hits         18446    18463      +17     
- Misses        4165     4173       +8     
  Partials       604      604              

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI review requested due to automatic review settings July 27, 2026 09:30

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
care/utils/evaluators/evaluation_metric/patient_age.py (1)

47-53: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Wire validate_rule into the runtime evaluation path.

interpretation_evaluator.py calls apply_rule, while EvaluationMetricBase.apply_rule dispatches directly to evaluate_* and does not invoke validate_rule. Consequently, inverted ranges and unsupported units can still reach evaluation—the new validation is unfortunately decorative unless this method is called before dispatch. Invoke validate_rule from apply_rule or validate the rule before calling it.

🤖 Prompt for 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.

In `@care/utils/evaluators/evaluation_metric/patient_age.py` around lines 47 - 53,
Invoke PatientAgeMetric.validate_rule through the runtime path before rule
evaluation: update EvaluationMetricBase.apply_rule, or the caller immediately
before it, so validation occurs before dispatching to evaluate_* methods.
Preserve the existing apply_rule dispatch behavior while ensuring inverted
ranges and unsupported units are rejected before evaluation.
🤖 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.

Outside diff comments:
In `@care/utils/evaluators/evaluation_metric/patient_age.py`:
- Around line 47-53: Invoke PatientAgeMetric.validate_rule through the runtime
path before rule evaluation: update EvaluationMetricBase.apply_rule, or the
caller immediately before it, so validation occurs before dispatching to
evaluate_* methods. Preserve the existing apply_rule dispatch behavior while
ensuring inverted ranges and unsupported units are rejected before evaluation.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: e5e389c6-efaf-4564-8ec7-60e72e824851

📥 Commits

Reviewing files that changed from the base of the PR and between 5e197e7 and a1b0d09.

📒 Files selected for processing (1)
  • care/utils/evaluators/evaluation_metric/patient_age.py

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 1 out of 1 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (2)

care/utils/evaluators/evaluation_metric/patient_age.py:35

  • RangeSpec currently only checks that min <= max; it still allows negative min/max values, which would produce nonsensical age ranges (and can lead to confusing behavior when evaluating rules). Consider rejecting negative values as part of the range validation.
    @model_validator(mode="after")
    def validate_range(self):
        if self.min > self.max:
            raise ValueError("min value cannot be greater than max value")
        return self

care/utils/evaluators/evaluation_metric/patient_age.py:24

  • ValueSpec allows negative exact ages (equality). If the intent is to validate patient ages, it should reject negative values to avoid storing impossible rules.
class ValueSpec(BaseModel):
    value: StrictInt
    value_type: ValueType = ValueType.years

Comment on lines +47 to +53
@classmethod
def validate_rule(cls, operation, value):
super().validate_rule(operation, value)
if operation == AllowedOperations.equality.value:
ValueSpec.model_validate(value)
elif operation == AllowedOperations.in_range.value:
RangeSpec.model_validate(value)
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.

2 participants