Skip to content

O3-5430: Validate obs datetime against patient DOB#5892

Open
BlessedAmrita wants to merge 1 commit intoopenmrs:masterfrom
BlessedAmrita:O3-5430-obs-dates-dob-validation
Open

O3-5430: Validate obs datetime against patient DOB#5892
BlessedAmrita wants to merge 1 commit intoopenmrs:masterfrom
BlessedAmrita:O3-5430-obs-dates-dob-validation

Conversation

@BlessedAmrita
Copy link

Description of what I changed

Added DOB lower-bound validation for obsDatetime in ObsValidator.

The obsDatetime field is validated against the patient's date of birth preventing, thus clinically impossible entries.

Changes:

  • ObsValidator.java: Added birthdate lower-bound check for obsDatetime using obs.getPerson().getBirthdate() with null guards
  • ObsValidatorTest.java: Added 5 test cases for DOB validation scenarios
  • messages.properties: Added Obs.error.obsDatetimeBeforePatientBirthdate key

Test scenarios:

  • obsDatetime before patient birthdate → validation error
  • obsDatetime equal to patient birthdate → no error
  • obsDatetime after patient birthdate → no error
  • obsDatetime is null → no DOB error
  • Patient birthdate is null → no error

Part of the Date Picker Improvements epic (O3-5424).

Issue I worked on

see https://issues.openmrs.org/browse/O3-5430

Checklist: I completed these to help reviewers :)

  • My IDE is configured to follow the code style of this project.
  • I have added tests to cover my changes.
  • I ran mvn clean package right before creating this pull request and
    added all formatting changes to my commit.
  • All new and existing tests passed.
  • My pull request is based on the latest changes of the master branch.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 5, 2026

@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 59.28%. Comparing base (dcf1695) to head (468c14c).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
.../main/java/org/openmrs/validator/ObsValidator.java 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #5892      +/-   ##
============================================
+ Coverage     59.26%   59.28%   +0.02%     
- Complexity     9251     9258       +7     
============================================
  Files           686      686              
  Lines         37219    37223       +4     
  Branches       5445     5447       +2     
============================================
+ Hits          22056    22066      +10     
+ Misses        13201    13196       -5     
+ Partials       1962     1961       -1     

☔ View full report in Codecov by Sentry.
📢 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.

errors.rejectValue("obsDatetime", "error.null");
}

if (obs.getObsDatetime() != null && obs.getPerson() != null && obs.getPerson().getBirthdate() != null
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably Wrong field being validated.

The ticket is specifically about the Expiration date and Next dose date fields in the Immunization App. Those values are stored in valueDatetime, not obsDatetime. obsDatetime is the timestamp of when the observation was recorded — that's a different concept entirely. Please change the validation to check valueDatetime against the patient's birthdate instead.

* <strong>Should</strong> fail validation if field lengths are not correct
* <strong>Should</strong> not validate if obs is voided
* <strong>Should</strong> not validate a voided child obs
* <strong>Should</strong> fail for a null object
Copy link
Contributor

Choose a reason for hiding this comment

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

//
strong>Should</strong> fail validation if valueDatetime is before the patient's date of birth
strong>Should</strong> pass validation if valueDatetime is on or after the patient's date of birth
strong>Should</strong> pass validation if valueDatetime is set but person has no birthdate
//

* @see ObsValidator#validate(java.lang.Object, org.springframework.validation.Errors)
*/
@Test
void validate_shouldRejectObsDatetimeBeforePatientBirthdate() {
Copy link
Contributor

Choose a reason for hiding this comment

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

should be public.

if (obs.getObsDatetime() != null && obs.getPerson() != null && obs.getPerson().getBirthdate() != null
&& obs.getObsDatetime().before(obs.getPerson().getBirthdate())) {
errors.rejectValue("obsDatetime", "Obs.error.obsDatetimeBeforePatientBirthdate",
"Obs datetime cannot be before the patient's date of birth");
Copy link
Contributor

Choose a reason for hiding this comment

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

remove hard coded message.

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