Skip to content

Commit efac896

Browse files
committed
whitespace; max length in patient surname
1 parent c4f4ed1 commit efac896

5 files changed

Lines changed: 27 additions & 3 deletions

File tree

backend/src/models/fhir_immunization_pre_validators.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,18 @@ def pre_validate_patient_name_given(self, values: dict) -> dict:
310310
except (KeyError, IndexError, AttributeError):
311311
pass
312312

313+
PERSON_SURNAME_MAX_LENGTH = 35
314+
313315
def pre_validate_patient_name_family(self, values: dict) -> dict:
314316
"""
315317
Pre-validate that, if a contained[?(@.resourceType=='Patient')].name[{index}].family (legacy CSV field name:
316-
PERSON_SURNAME) exists, index dynamically determined then it is a an array containing a single non-empty string
318+
PERSON_SURNAME) exists, index dynamically determined then it is a non-empty string of maximum length
319+
35 characters
317320
"""
318321
field_location = patient_name_family_field_location(values)
319322
try:
320323
field_value, _ = patient_and_practitioner_value_and_index(values, "family", "Patient")
321-
PreValidation.for_string(field_value, field_location)
324+
PreValidation.for_string(field_value, field_location, max_length=self.PERSON_SURNAME_MAX_LENGTH)
322325
except (KeyError, IndexError, AttributeError):
323326
pass
324327

backend/src/models/utils/pre_validator_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ def for_string(
2323
if not isinstance(field_value, str):
2424
raise TypeError(f"{field_location} must be a string")
2525

26+
if field_value.isspace():
27+
raise ValueError(f"{field_location} must not be whitespace")
28+
2629
if defined_length:
2730
if len(field_value) != defined_length:
2831
raise ValueError(f"{field_location} must be {defined_length} characters")

backend/tests/test_immunization_pre_validator.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,9 @@ def test_pre_validate_patient_name_family(self):
506506
ValidatorModelTests.test_string_value(
507507
self,
508508
field_location=patient_name_family_field_location(valid_json_data),
509-
valid_strings_to_test=["test"],
509+
valid_strings_to_test=["test", "Quitelongsurname", "Surnamewithjustthirtyfivecharacters"],
510+
max_length=PreValidators.PERSON_SURNAME_MAX_LENGTH,
511+
invalid_length_strings_to_test=["Surnamethathasgotthirtysixcharacters"],
510512
)
511513

512514
def test_pre_validate_patient_birth_date(self):

backend/tests/testing_utils/pre_validation_test_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ def test_string_value(
7676
expected_error_message=f"{field_location} must be a string",
7777
)
7878

79+
# Test whitespace
80+
for invalid_whitespace_string in InvalidValues.for_whitespace_strings:
81+
test_invalid_values_rejected(
82+
test_instance,
83+
valid_json_data,
84+
field_location=field_location,
85+
invalid_value=invalid_whitespace_string,
86+
expected_error_message=f"{field_location} must not be whitespace",
87+
)
88+
7989
# If there is a predefined string length, then test invalid string lengths,
8090
# otherwise check the empty string only
8191
if defined_length:

backend/tests/testing_utils/values_for_tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ class Invalid:
319319
class InvalidValues:
320320
"""Store lists of invalid values for tests"""
321321

322+
for_whitespace_strings = [
323+
" ", # All spaces
324+
" \n ", # Spaces and newlines
325+
"\r\n\t", # CR, LF and tabs
326+
]
327+
322328
for_postal_codes = [
323329
"SW1 1AA", # Too many spaces in divider
324330
"SW 1 1A", # Too many space dividers

0 commit comments

Comments
 (0)