Configuration¶
A few behaviors can be changed for your whole application. All of them are
global, so set them once at startup, before importing any
fhir.resources model, for example in your package’s __init__.py.
Global model config¶
By default, models are configured with:
>>> from fhir.resources.patient import Patient
>>> dict(Patient.model_config)
{'extra': 'forbid', 'validate_assignment': True, 'populate_by_name': True}
FHIRAbstractModel.set_global_model_config overrides any
pydantic.ConfigDict key for every model defined afterwards. For
example, to silently ignore unknown elements instead of rejecting them:
# myapp/__init__.py — must run before any fhir.resources model is imported
from fhir_core.fhirabstractmodel import FHIRAbstractModel
FHIRAbstractModel.set_global_model_config(extra="ignore")
from fhir.resources.patient import Patient # noqa: E402
Patient.model_validate({"resourceType": "Patient", "nickname": "Ad"}) # OK
Models that are already imported keep their original configuration.
FHIRAbstractModel.get_global_model_config() shows the current settings, and
reset_global_model_config() restores the defaults for models defined later.
Maximum length of id¶
The FHIR specification limits id to 64 characters. Because many real
systems use longer ids, fhir.resources allows up to 255 by default.
You can change this limit:
import importlib
from fhir_core import constraints
from fhir_core import types as ftypes
constraints.TYPES_ID_MAX_LENGTH = 64
importlib.reload(ftypes)
We recommend keeping ids within 64 characters for interoperability. Earlier discussions are on the FHIR chat and Jira.
Allowing empty strings¶
FHIR forbids empty strings ("") in primitive values. If you must accept
real-world data that contains them:
import importlib
from fhir_core import constraints
from fhir_core import types as ftypes
constraints.TYPES_STRING_ALLOW_EMPTY_STR = True
importlib.reload(ftypes)
Warning
These monkey patches change behavior for the whole process, including
third-party libraries that use fhir.resources.