nazrulworld / fhir.resources

FHIR Resources https://www.hl7.org/fhir/resourcelist.html
https://pypi.org/project/fhir.resources/
Other
365 stars 104 forks source link

Validation error with a split primitive #97

Open azmeuk opened 2 years ago

azmeuk commented 2 years ago

Hello,

Using fhir-kindling on production data, I encountered a validation error with an Organization resource which address line is split between line and line_. The HL7 specification on JSON representation of primitive elements seems to indicate that the data I try to read is valid:

FHIR elements with primitive datatypes are represented in two parts:

A JSON property with the name of the element, which has a JSON type of number, boolean, or string a JSON property with _ prepended to the name of the element, which, if present, contains the value's id and/or extensions

The data I handled comes from the French government health public data API. I have asked them about this and they say their data is valid.

I have cleaned up the data so the following snippet is readable. You can copy/paste this in a REPL to reproduce the exception:

>>> from fhir.resources.organization import Organization
>>> Organization(**{
...   "resourceType": "Organization",
...   "address": [
...     {
...       "line": [
...         None
...       ],
...       "_line": [
...         {
...           "extension": [
...             {
...               "url": "http://hl7.org/fhir/StructureDefinition/iso21090-ADXP-houseNumber",
...               "valueString": "80"
...             },
...             {
...               "url": "http://hl7.org/fhir/StructureDefinition/iso21090-ADXP-streetNameType",
...               "valueString": "AV"
...             },
...             {
...               "url": "http://hl7.org/fhir/StructureDefinition/iso21090-ADXP-streetNameBase",
...               "valueString": "FOOBAR"
...             },
...           ]
...         }
...       ],
...     }
...   ]
... })
Traceback (most recent call last):
  File ".../env/lib/python3.10/site-packages/fhir/resources/fhirabstractmodel.py", line 105, in __init__
    BaseModel.__init__(__pydantic_self__, **data)
  File "pydantic/main.py", line 331, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for Organization
address -> 0 -> line -> 0
  none is not an allowed value (type=type_error.none.not_allowed)

1 validation error for Organization
address -> 0 -> line -> 0
  none is not an allowed value (type=type_error.none.not_allowed)

Is my data wrong in the end, or is this something fhir.resources does not support yet?

Thank you for your help

nazrulworld commented 2 years ago

Hi @azmeuk, Firstly thanks a lot for this extraordinary issue!

  1. According to our current implementation, the member line value (list of strings) cannot be None or empty string! But you can provide an empty list or None (not None inside the list). In your example, if you remove None from the list, it will not give you a validation error. Or instead of None, you can use any fixed string to prevent validation error.
  2. I tried to Create an Organization at http://hapi.fhir.org/create with your data and it is working. So I think None is allowed inside a list.
  3. For more confirmation, I added the issue here https://chat.fhir.org/#narrow/stream/179166-implementers/topic/Address.2Eline
  4. It will be breaking business logic changes if we want to support (None as a member of the list), but I think we should. It will take a little bit more time as I am currently busy with professional work. I will try to manage time as soon as possible but in the meanwhile any PR is welcome.