From 5e197e7507dfe4a2befe89568cb0692094c018fd Mon Sep 17 00:00:00 2001 From: nandkishorr Date: Mon, 27 Jul 2026 13:09:34 +0530 Subject: [PATCH 1/2] fix:added age range validation for monetary component --- .../evaluation_metric/patient_age.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/care/utils/evaluators/evaluation_metric/patient_age.py b/care/utils/evaluators/evaluation_metric/patient_age.py index 8a33600749..d846279ff3 100644 --- a/care/utils/evaluators/evaluation_metric/patient_age.py +++ b/care/utils/evaluators/evaluation_metric/patient_age.py @@ -2,6 +2,7 @@ from dateutil.relativedelta import relativedelta from django.utils import timezone +from pydantic import BaseModel, StrictInt from care.utils.evaluators.evaluation_metric.base import EvaluationMetricBase from care.utils.registries.evaluation_metric import ( @@ -10,6 +11,17 @@ ) +class ValueSpec(BaseModel): + value: StrictInt + value_type: str = "years" + + +class RangeSpec(BaseModel): + min: StrictInt + max: StrictInt + value_type: str = "years" + + class PatientAgeMetric(EvaluationMetricBase): context = "patient" name = "patient_age" @@ -19,6 +31,14 @@ class PatientAgeMetric(EvaluationMetricBase): AllowedOperations.equality.value, ] + @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) + def get_value(self): start = self.context_object.date_of_birth or date( self.context_object.year_of_birth, 1, 1 From a1b0d0900e4c82af93012d20997abe55f92ec83a Mon Sep 17 00:00:00 2001 From: nandkishorr Date: Mon, 27 Jul 2026 15:00:18 +0530 Subject: [PATCH 2/2] fix:added review changes --- .../evaluation_metric/patient_age.py | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/care/utils/evaluators/evaluation_metric/patient_age.py b/care/utils/evaluators/evaluation_metric/patient_age.py index d846279ff3..5ae9f40b76 100644 --- a/care/utils/evaluators/evaluation_metric/patient_age.py +++ b/care/utils/evaluators/evaluation_metric/patient_age.py @@ -1,8 +1,9 @@ from datetime import date +from enum import Enum from dateutil.relativedelta import relativedelta from django.utils import timezone -from pydantic import BaseModel, StrictInt +from pydantic import BaseModel, StrictInt, model_validator from care.utils.evaluators.evaluation_metric.base import EvaluationMetricBase from care.utils.registries.evaluation_metric import ( @@ -11,15 +12,27 @@ ) +class ValueType(str, Enum): + years = "years" + months = "months" + days = "days" + + class ValueSpec(BaseModel): value: StrictInt - value_type: str = "years" + value_type: ValueType = ValueType.years class RangeSpec(BaseModel): min: StrictInt max: StrictInt - value_type: str = "years" + value_type: ValueType = ValueType.years + + @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 class PatientAgeMetric(EvaluationMetricBase): @@ -47,11 +60,11 @@ def get_value(self): return relativedelta(end, start).normalized() def convert_value_to_units(self, value, value_type): - if value_type == "years": + if value_type == ValueType.years: return value.years - if value_type == "months": + if value_type == ValueType.months: return value.years * 12 + value.months - if value_type == "days": + if value_type == ValueType.days: return value.years * 365 + value.months * 30 + value.days raise ValueError("Invalid value type")