(fix) O3-5272:Add validation for contact details address fields#2180
(fix) O3-5272:Add validation for contact details address fields#2180praneeth622 wants to merge 7 commits intoopenmrs:mainfrom
Conversation
- Add validation for city/village, state/province, and country fields to reject numeric-only input and special characters - Add validation for postal code to support international formats (alphanumeric with spaces and hyphens) - Reject invalid characters while supporting international Unicode characters (Khmer, Arabic, etc.) - Add 25+ comprehensive test cases covering valid/invalid inputs - All address fields remain optional but validate when data is entered - Support common punctuation (hyphens, apostrophes, periods, parentheses) Fixes: https://openmrs.atlassian.net/browse/O3-5272
There was a problem hiding this comment.
Pull request overview
This PR adds comprehensive validation for address fields in the patient registration form's Contact Details section to prevent invalid input. The implementation adds Yup validation rules for city/village, state/province, country, county/district, and postal code fields.
Key changes include:
- Validation rules for geographic address fields (city, state, country, county/district) that allow Unicode letters, spaces, hyphens, apostrophes, periods, and parentheses
- Postal code validation supporting international formats (alphanumeric, spaces, hyphens)
- 25+ test cases covering valid inputs, invalid inputs, edge cases, and international formats
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 12 comments.
| File | Description |
|---|---|
packages/esm-patient-registration-app/src/patient-registration/validation/patient-registration-validation.ts |
Adds Yup validation schema for address fields with regex patterns to validate city, state, country, county/district, and postal code inputs |
packages/esm-patient-registration-app/src/patient-registration/validation/patient-registration-validation.test.ts |
Adds comprehensive test suite with 25+ test cases covering valid/invalid inputs, international characters, and edge cases for all address fields |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
...registration-app/src/patient-registration/validation/patient-registration-validation.test.ts
Show resolved
Hide resolved
...registration-app/src/patient-registration/validation/patient-registration-validation.test.ts
Show resolved
Hide resolved
...ient-registration-app/src/patient-registration/validation/patient-registration-validation.ts
Outdated
Show resolved
Hide resolved
...ient-registration-app/src/patient-registration/validation/patient-registration-validation.ts
Outdated
Show resolved
Hide resolved
...ient-registration-app/src/patient-registration/validation/patient-registration-validation.ts
Outdated
Show resolved
Hide resolved
...registration-app/src/patient-registration/validation/patient-registration-validation.test.ts
Show resolved
Hide resolved
...ient-registration-app/src/patient-registration/validation/patient-registration-validation.ts
Outdated
Show resolved
Hide resolved
...ient-registration-app/src/patient-registration/validation/patient-registration-validation.ts
Outdated
Show resolved
Hide resolved
...ient-registration-app/src/patient-registration/validation/patient-registration-validation.ts
Outdated
Show resolved
Hide resolved
...ient-registration-app/src/patient-registration/validation/patient-registration-validation.ts
Outdated
Show resolved
Hide resolved
- Update validation error messages to accurately reflect allowed characters (periods and parentheses)
- Remove redundant Khmer Unicode range (\u1780-\u17FF) from regex patterns as \p{L} already covers all Unicode letters
- Simplify regex patterns to /^[\p{L}\s\-'.()]+$/u for better maintainability
- Add missing test cases:
* City/village with parentheses
* State/province with parentheses
* Country with periods (e.g., U.S.A.)
* Country with parentheses
* County/district with periods, apostrophes, and parentheses (e.g., St. Mary's County)
- Update all test expectations to match new error messages
- Total test cases now: 30+ covering all edge cases
All suggestions from GitHub Copilot review have been addressed.
- Add \p{M} (Unicode combining marks) to all address field regex patterns
- This fixes validation for scripts that use combining characters like:
* Khmer (e.g., កម្ពុជា uses combining marks like ្, ុ, ា)
* Arabic (diacritical marks)
* Hebrew (vowel points and cantillation marks)
- Pattern now: /^[\p{L}\p{M}\s\-'.()]+$/u
- Ensures international address validation works properly
- All tests passing (48 validation tests + 3 integration tests)
| (value) => { | ||
| if (!value) return true; // Allow empty values since field is optional | ||
| // Allow alphanumeric characters, spaces, and hyphens to support international postal codes | ||
| return /^[A-Za-z0-9\s\-]+$/.test(value); |
There was a problem hiding this comment.
@praneeth622 Do we want postal codes to support international alphanumeric formats, or should this be numeric-only as per the PR requirement ? Current validation supports the former.
| }, | ||
| ), | ||
| country: Yup.string() | ||
| .optional() |
There was a problem hiding this comment.
@praneeth622 The fields being optional only allows them to be empty. If a value is entered, invalid formats should still trigger validation and prevent saving, which doesn’t seem to be enforced currently
|
Thanks for the review, @gitcliff 👍
I’m happy to update the validation accordingly.
I’ll push an update once the expected postal code format is confirmed. Thanks! |
|
Hello, I’ve reviewed this issue and understood the validation requirements for the address fields (City, State, Country, and Postal Code). I plan to handle this by applying proper regex-based validation for international characters, allowing optional fields while blocking invalid inputs, and ensuring the form does not submit when values are incorrect. I’d be happy to work on this — could you please assign the issue to me? |
|
Hi, just following up on this. I’ve already prepared the fix and tests locally and I’m ready to submit as soon as |
There was a problem hiding this comment.
Thanks for the PR, @praneeth622. really appreciate you digging into this!
Just to share a bit more context: the address fields in OpenMRS are configurable. That means they’re dynamically loaded based on something called Address Templates. Ex: one implementation might include a “Postal Code” field, while another might not. It all depends on how the template is defined.
The good news is that validation rules can also be defined within the same template. (However the default template we’re currently using doesn’t include any regex-based validation rules, but it can be configured openmrs/openmrs-core@12b454b/api/src/main/java/org/openmrs/util/OpenmrsConstants.java#L380).
Read more on Address Templates:
openmrs.atlassian.net/wiki/spaces/docs/pages/25471961/Administering+Address+Templates
As a next step for this PR, we’ll want to confirm whether the frontend currently supports dynamic validation rules for address fields. If it doesn’t we’ll need to add support for that so it aligns with how address templates are configured.
Keep up the good work. You are doing great! 💛
|
Thanks for the clarification @jayasanka-sack . I now understand that address fields are template-driven and validation rules may be defined in elementRegex. I’ll investigate whether frontend supports dynamic validation from address templates and propose an aligned solution. |
Requirements
Summary
This PR adds proper validation for address fields in the patient registration form's Contact Details section to fix O3-5272.
Problem: Previously, the Contact Details address fields (City/Village, State/Province, Country, Postal Code) accepted invalid input without any validation. Users could enter numeric-only values like
12345for State, special characters likeindia123#$for City, or invalid characters in Postal Code like503144ferge#$, and the form would save successfully.Solution: Implemented comprehensive validation rules for address fields:
Validation Rules Added:
City/Village, State/Province, Country, County/District:
12345), special characters (e.g.,city#$%)New York,São Paulo,O'Brien,Saint-Denis,កំពង់ចាម,Guinea-BissauPostal Code:
12345,12345-6789(US),SW1A 1AA(UK),K1A 0B1(Canada)Key Features:
\p{L})Test Coverage:
patient-registration-validation.test.tsScreenshots
Before:
Form accepted invalid input without validation:

After:
Validation errors displayed for invalid input:
Test Case 1: Numeric-only State
12345Test Case 2: Special characters in City
india123#$Test Case 3: Invalid Postal Code
503144ferge#$Test Case 4: Valid International Input
កំពង់ចាម(Khmer), State =São Paulo(Portuguese), Postal Code =SW1A 1AA(UK format)Related Issue
Fixes : #O3-5272
https://openmrs.atlassian.net/browse/O3-5272
Part of Epic: https://openmrs.atlassian.net/browse/O3-4257 (React Form Engine v4)