Skip to content

O3-5425: Validate order dates against patient DOB#5887

Open
BlessedAmrita wants to merge 1 commit intoopenmrs:masterfrom
BlessedAmrita:O3-5425-order-dates-dob-validation
Open

O3-5425: Validate order dates against patient DOB#5887
BlessedAmrita wants to merge 1 commit intoopenmrs:masterfrom
BlessedAmrita:O3-5425-order-dates-dob-validation

Conversation

@BlessedAmrita
Copy link

@BlessedAmrita BlessedAmrita commented Mar 4, 2026

Description of what I changed

Added DOB lower-bound validation for dateActivated and scheduledDate in OrderValidator.

Since these date fields live on the base Order class, placing the validation in OrderValidator means it automatically applies to all order types: TestOrder, DrugOrder, ReferralOrder, etc. When either date falls before the patient's date of birth, the validator rejects it with a field error. Dates equal to the birthdate are allowed.

Changes:

  • OrderValidator.java: Added birthdate lower-bound checks in validateDateActivated() and validateScheduledDate() methods, with null guards for patient and birthdate
  • OrderValidatorTest.java: Added 10 test cases (5 per date field) covering before/equal/after birthdate, null date, and null birthdate scenarios
  • messages.properties: Added Order.error.dateActivatedBeforePatientBirthdate and Order.error.scheduledDateBeforePatientBirthdate keys

Test scenarios (for each date field):

  • Date before patient birthdate → validation error
  • Date equal to patient birthdate → no error
  • Date after patient birthdate → no error
  • Date is null → no 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-5425

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. (If you refactored existing code that was well tested you do not have to add tests)
  • 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 4, 2026

@sagarkhandagre998
Copy link
Contributor

sagarkhandagre998 commented Mar 9, 2026

@BlessedAmrita, apologies for the earlier incorrect feedback. After reviewing the maintainer's guidance more carefully, your approach of validating dates against the patient's date of birth is actually the right direction. The maintainer confirmed that the patient's DOB is the natural lower bound for these dates, which inherently catches single-digit year entries like "02/01/1" since year 1 will always be before any real patient's birthdate.

errors.rejectValue("dateActivated", "Order.error.encounterDatetimeAfterDateActivated");
}
if (order.getPatient() != null && order.getPatient().getBirthdate() != null
&& dateActivated.before(order.getPatient().getBirthdate())) {
Copy link
Contributor

Choose a reason for hiding this comment

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

One edge case to consider —> in OpenMRS, a patient's birthdate can be estimated (birthdateEstimated = true), When this is the case, the stored birthdate is not an exact date —> it is a synthetic value calculated from the patient's approximate age.

Would it could cause false rejections?

Reference - visitValidator

}
Date scheduledDate = order.getScheduledDate();
if (scheduledDate != null && order.getPatient() != null && order.getPatient().getBirthdate() != null
&& scheduledDate.before(order.getPatient().getBirthdate())) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here.

if (scheduledDate != null && order.getPatient() != null && order.getPatient().getBirthdate() != null
&& scheduledDate.before(order.getPatient().getBirthdate())) {
errors.rejectValue("scheduledDate", "Order.error.scheduledDateBeforePatientBirthdate",
"Scheduled date cannot be before patient's birthdate");
Copy link
Contributor

Choose a reason for hiding this comment

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

You can remove the 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.

2 participants