33from pydantic import ValidationError
44from pydantic_core import ErrorDetails
55
6- from nrlf .core .constants import CONTENT_FORMAT_CODE_URL , CONTENT_STABILITY_SYSTEM_URL
76from nrlf .core .response import Response
87from nrlf .core .types import CodeableConcept
98from nrlf .producer .fhir .r4 import model as producer_model
@@ -20,28 +19,25 @@ def format_error_location(loc: List) -> str:
2019 return formatted_loc
2120
2221
23- def append_value_set_url (loc_string : str ) -> str :
24- if loc_string .endswith (("url" , "system" )):
25- return ""
26-
27- if "content" in loc_string :
28- if "extension" in loc_string :
29- return f". See ValueSet: { CONTENT_STABILITY_SYSTEM_URL } "
30- if "format" in loc_string :
31- return f". See ValueSet: { CONTENT_FORMAT_CODE_URL } "
32-
33- return ""
34-
35-
36- def diag_for_error (error : ErrorDetails ) -> str :
22+ def diag_for_error (error : ErrorDetails , value_set : str , root_location : tuple ) -> str :
3723 loc_string = format_error_location (error ["loc" ])
24+ if root_location :
25+ loc_string = format_error_location (root_location ) + "." + loc_string
26+
3827 msg = f"{ loc_string or 'DocumentReference' } : { error ['msg' ]} "
39- msg += append_value_set_url ( loc_string )
28+ msg += f", see: { value_set } " if value_set else ""
4029 return msg
4130
4231
43- def expression_for_error (error : ErrorDetails ) -> Optional [str ]:
44- return format_error_location (error ["loc" ]) or "DocumentReference"
32+ def expression_for_error (error : ErrorDetails , root_location : tuple ) -> Optional [str ]:
33+ loc_string = format_error_location (error ["loc" ]) or "DocumentReference"
34+ if root_location and error ["loc" ]:
35+ loc_string = (
36+ format_error_location (root_location )
37+ + "."
38+ + format_error_location (error ["loc" ])
39+ )
40+ return loc_string
4541
4642
4743class OperationOutcomeError (Exception ):
@@ -91,15 +87,20 @@ def __init__(self, issues: List[OperationOutcomeIssue]):
9187
9288 @classmethod
9389 def from_validation_error (
94- cls , exc : ValidationError , details : CodeableConcept , msg : str = ""
90+ cls ,
91+ exc : ValidationError ,
92+ details : CodeableConcept ,
93+ msg : str = "" ,
94+ value_set : str = "" ,
95+ root_location : tuple = None ,
9596 ):
9697 issues = [
9798 producer_model .OperationOutcomeIssue (
9899 severity = "error" ,
99100 code = "invalid" ,
100101 details = details , # type: ignore
101- diagnostics = f"{ msg } ({ diag_for_error (error )} )" ,
102- expression = [expression_for_error (error )], # type: ignore
102+ diagnostics = f"{ msg } ({ diag_for_error (error , value_set , root_location )} )" ,
103+ expression = [expression_for_error (error , root_location )], # type: ignore
103104 )
104105 for error in exc .errors ()
105106 ]
0 commit comments