nazrulworld / fhir.resources

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

Any resource: "id" element fails validation with underscore in value #55

Closed sbwhitney closed 3 years ago

sbwhitney commented 3 years ago

Description

If resource.id element has an underscore, validation fails

What I Did

resource = {'resourceType': 'Patient', 'id': 'abc_1234'}
Patient(**resource)

pydantic.error_wrappers.ValidationError: 1 validation error for Patient id string does not match regex "^[A-Za-z0-9-.]+$" (type=value_error.str.regex; pattern=^[A-Za-z0-9-.]+$)

sbwhitney commented 3 years ago

@nazrulworld I can make a PR request to update this line:

https://github.com/nazrulworld/fhir.resources/blob/9e81ed05607389158f5fc997d9a3ab19bd18e6e1/fhir/resources/fhirtypes.py#L136

But would you want to update for all FHIR versions?

^[A-Za-z0-9\-_.]+$ would be the new regex. Also, would I branch off master?

Edit: I receive forbidden error when trying to push new branch to remote:

remote: Permission to nazrulworld/fhir.resources.git denied to sbwhitney. fatal: unable to access 'https://github.com/nazrulworld/fhir.resources.git/': The requested URL returned error: 403

Update: I can see that the regex is configurable. https://github.com/nazrulworld/fhir.resources#resourceid-aka-fhirtypesid-constraint-extensibility

nazrulworld commented 3 years ago

First of all @sbwhitney thanks a lot for this issue. If you look at FHIR specification https://www.hl7.org/fhir/datatypes.html#id by default underscore (_) is not allowed. So the default behavior is regex = re.compile(r"^[A-Za-z0-9\-.]+$") (should be if your system communicate to other system) But it is possible to override this behavior. https://github.com/nazrulworld/fhir.resources#resourceid-aka-fhirtypesid-constraint-extensibility

>>> from fhir.resources.fhirtypes import Id
>>> Id.configure_constraints(regex=re.compile(r"^[A-Za-z0-9\-_.]+$"))

For PR.

  1. fork this repo
  2. Make PR Hopefully should work, if still, you are facing a problem let me know.
sbwhitney commented 3 years ago

Thanks for the info @nazrulworld. Much appreciated. No PR necessary as underscore (_) is disallowed.