Skip to content

Commit aca3ca4

Browse files
NRL-518 Add value set for pydantic diagnostic messages
1 parent 5098eca commit aca3ca4

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

layer/nrlf/core/errors.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,25 @@ def format_error_location(loc: List) -> str:
1919
return formatted_loc
2020

2121

22+
def append_value_set_url(loc_string: str) -> str:
23+
if loc_string.endswith(("url", "system")):
24+
return ""
25+
26+
if "content" in loc_string:
27+
if "extension" in loc_string:
28+
return ". See ValueSet: https://fhir.nhs.uk/England/CodeSystem/England-NRLContentStability"
29+
if "format" in loc_string:
30+
return ". See ValueSet: https://fhir.nhs.uk/England/CodeSystem/England-NRLFormatCode"
31+
32+
return ""
33+
34+
2235
def diag_for_error(error: ErrorDetails) -> str:
2336
loc_string = format_error_location(error["loc"])
24-
return f"{loc_string or 'root'}: {error['msg']}"
37+
print(f"Error location: {loc_string}")
38+
msg = f"{loc_string or 'root'}: {error['msg']}"
39+
msg += append_value_set_url(loc_string)
40+
return msg
2541

2642

2743
def expression_for_error(error: ErrorDetails) -> Optional[str]:

layer/nrlf/core/tests/test_pydantic_errors.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_validate_content_missing_format():
8484
}
8585
]
8686
},
87-
"diagnostics": "Failed to parse DocumentReference resource (content[0].format: Field required)",
87+
"diagnostics": "Failed to parse DocumentReference resource (content[0].format: Field required. See ValueSet: https://fhir.nhs.uk/England/CodeSystem/England-NRLFormatCode)",
8888
"expression": ["content[0].format"],
8989
}
9090

@@ -115,7 +115,7 @@ def test_validate_content_multiple_content_stability_extensions():
115115
}
116116
]
117117
},
118-
"diagnostics": "Failed to parse DocumentReference resource (content[0].extension: List should have at most 1 item after validation, not 2)",
118+
"diagnostics": "Failed to parse DocumentReference resource (content[0].extension: List should have at most 1 item after validation, not 2. See ValueSet: https://fhir.nhs.uk/England/CodeSystem/England-NRLContentStability)",
119119
"expression": ["content[0].extension"],
120120
}
121121

@@ -145,7 +145,7 @@ def test_validate_content_invalid_content_stability_code():
145145
}
146146
]
147147
},
148-
"diagnostics": "Failed to parse DocumentReference resource (content[0].extension[0].valueCodeableConcept.coding[0].code: Input should be 'static' or 'dynamic')",
148+
"diagnostics": "Failed to parse DocumentReference resource (content[0].extension[0].valueCodeableConcept.coding[0].code: Input should be 'static' or 'dynamic'. See ValueSet: https://fhir.nhs.uk/England/CodeSystem/England-NRLContentStability)",
149149
"expression": ["content[0].extension[0].valueCodeableConcept.coding[0].code"],
150150
}
151151

@@ -175,7 +175,7 @@ def test_validate_content_invalid_content_stability_display():
175175
}
176176
]
177177
},
178-
"diagnostics": "Failed to parse DocumentReference resource (content[0].extension[0].valueCodeableConcept.coding[0].display: Input should be 'Static' or 'Dynamic')",
178+
"diagnostics": "Failed to parse DocumentReference resource (content[0].extension[0].valueCodeableConcept.coding[0].display: Input should be 'Static' or 'Dynamic'. See ValueSet: https://fhir.nhs.uk/England/CodeSystem/England-NRLContentStability)",
179179
"expression": [
180180
"content[0].extension[0].valueCodeableConcept.coding[0].display"
181181
],
@@ -267,7 +267,7 @@ def test_validate_content_empty_content_stability_coding():
267267
}
268268
]
269269
},
270-
"diagnostics": "Failed to parse DocumentReference resource (content[0].extension[0].valueCodeableConcept.coding: List should have at least 1 item after validation, not 0)",
270+
"diagnostics": "Failed to parse DocumentReference resource (content[0].extension[0].valueCodeableConcept.coding: List should have at least 1 item after validation, not 0. See ValueSet: https://fhir.nhs.uk/England/CodeSystem/England-NRLContentStability)",
271271
"expression": ["content[0].extension[0].valueCodeableConcept.coding"],
272272
}
273273

@@ -298,6 +298,6 @@ def test_validate_content_missing_content_stability_coding():
298298
}
299299
]
300300
},
301-
"diagnostics": "Failed to parse DocumentReference resource (content[0].extension[0].valueCodeableConcept.coding: Field required)",
301+
"diagnostics": "Failed to parse DocumentReference resource (content[0].extension[0].valueCodeableConcept.coding: Field required. See ValueSet: https://fhir.nhs.uk/England/CodeSystem/England-NRLContentStability)",
302302
"expression": ["content[0].extension[0].valueCodeableConcept.coding"],
303303
}

0 commit comments

Comments
 (0)