Skip to content

Commit 3d8f0bd

Browse files
NRL-518 Improve pydantic diagnostic message
1 parent 07fd1e3 commit 3d8f0bd

File tree

5 files changed

+51
-16
lines changed

5 files changed

+51
-16
lines changed

layer/nrlf/core/errors.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,23 @@
99
from nrlf.producer.fhir.r4.model import OperationOutcome, OperationOutcomeIssue
1010

1111

12+
def format_error_location(loc: List) -> str:
13+
formatted_loc = ""
14+
for each in loc:
15+
if isinstance(each, int):
16+
formatted_loc = f"{formatted_loc}[{each}]"
17+
else:
18+
formatted_loc = f"{formatted_loc}.{each}" if formatted_loc else str(each)
19+
return formatted_loc
20+
21+
1222
def diag_for_error(error: ErrorDetails) -> str:
13-
if error["loc"]:
14-
loc_string = ".".join(str(each) for each in error["loc"])
15-
return f"{loc_string}: {error['msg']}"
16-
else:
17-
return f"root: {error['msg']}"
23+
loc_string = format_error_location(error["loc"])
24+
return f"{loc_string}: {error['msg']}" if loc_string else f"root: {error['msg']}"
1825

1926

2027
def expression_for_error(error: ErrorDetails) -> Optional[str]:
21-
return str(".".join(str(each) for each in error["loc"]) if error["loc"] else "root")
28+
return format_error_location(error["loc"]) or "root"
2229

2330

2431
class OperationOutcomeError(Exception):

layer/nrlf/core/tests/test_pydantic_errors.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def test_validate_content_missing_attachment():
2828
}
2929
]
3030
},
31-
"diagnostics": "Failed to parse DocumentReference resource (content.0.attachment: Field required)",
32-
"expression": ["content.0.attachment"],
31+
"diagnostics": "Failed to parse DocumentReference resource (content[0].attachment: Field required)",
32+
"expression": ["content[0].attachment"],
3333
}
3434

3535

@@ -56,6 +56,34 @@ def test_validate_content_missing_content_type():
5656
}
5757
]
5858
},
59-
"diagnostics": "Failed to parse DocumentReference resource (content.0.attachment.contentType: Field required)",
60-
"expression": ["content.0.attachment.contentType"],
59+
"diagnostics": "Failed to parse DocumentReference resource (content[0].attachment.contentType: Field required)",
60+
"expression": ["content[0].attachment.contentType"],
61+
}
62+
63+
64+
def test_validate_content_missing_format():
65+
validator = DocumentReferenceValidator()
66+
document_ref_data = load_document_reference_json("Y05868-736253002-Valid")
67+
68+
document_ref_data["content"][0].pop("format")
69+
70+
with pytest.raises(ParseError) as error:
71+
validator.validate(document_ref_data)
72+
73+
exc = error.value
74+
assert len(exc.issues) == 1
75+
assert exc.issues[0].model_dump(exclude_none=True) == {
76+
"severity": "error",
77+
"code": "invalid",
78+
"details": {
79+
"coding": [
80+
{
81+
"system": "https://fhir.nhs.uk/ValueSet/Spine-ErrorOrWarningCode-1",
82+
"code": "INVALID_RESOURCE",
83+
"display": "Invalid validation of resource",
84+
}
85+
]
86+
},
87+
"diagnostics": "Failed to parse DocumentReference resource (content[0].format: Field required)",
88+
"expression": ["content[0].format"],
6189
}

tests/features/producer/createDocumentReference-failure.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,9 +725,9 @@ Feature: Producer - createDocumentReference - Failure Scenarios
725725
}
726726
]
727727
},
728-
"diagnostics": "Request body could not be parsed (content.0.attachment.contentType: String should match pattern '[^\\s]+(\\s[^\\s]+)*')",
728+
"diagnostics": "Request body could not be parsed (content[0].attachment.contentType: String should match pattern '[^\\s]+(\\s[^\\s]+)*')",
729729
"expression": [
730-
"content.0.attachment.contentType"
730+
"content[0].attachment.contentType"
731731
]
732732
}
733733
"""

tests/features/producer/updateDocumentReference-failure.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ Feature: Producer - updateDocumentReference - Failure Scenarios
144144
}
145145
]
146146
},
147-
"diagnostics": "Request body could not be parsed (content.0.attachment.contentType: String should match pattern '[^\\s]+(\\s[^\\s]+)*')",
147+
"diagnostics": "Request body could not be parsed (content[0].attachment.contentType: String should match pattern '[^\\s]+(\\s[^\\s]+)*')",
148148
"expression": [
149-
"content.0.attachment.contentType"
149+
"content[0].attachment.contentType"
150150
]
151151
}
152152
"""

tests/features/producer/upsertDocumentReference-failure.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,9 @@ Feature: Producer - upsertDocumentReference - Failure Scenarios
306306
}
307307
]
308308
},
309-
"diagnostics": "Request body could not be parsed (content.0.attachment.contentType: String should match pattern '[^\\s]+(\\s[^\\s]+)*')",
309+
"diagnostics": "Request body could not be parsed (content[0].attachment.contentType: String should match pattern '[^\\s]+(\\s[^\\s]+)*')",
310310
"expression": [
311-
"content.0.attachment.contentType"
311+
"content[0].attachment.contentType"
312312
]
313313
}
314314
"""

0 commit comments

Comments
 (0)