FHIR Types Utilities
Utility classes and functions for getting and validating FHIR types.
FHIRTypeError
Path: fhircraft.fhir.resources.datatypes.utils.FHIRTypeError
Bases: Exception
Raised when type checking or conversion fails.
get_complex_FHIR_type
Source code in fhircraft/fhir/resources/datatypes/utils.py
get_fhir_primitive_type
get_fhir_resource_type
Source code in fhircraft/fhir/resources/datatypes/utils.py
get_fhir_type
Get the FHIR type (primitive, complex, or resource) by its string name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_str
|
str
|
The FHIR type name. |
required |
release
|
str
|
The FHIR release version (default: "R4B"). |
'R4B'
|
Returns:
| Name | Type | Description |
|---|---|---|
type |
Any
|
The corresponding FHIR type class. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If the type is not found. |
Source code in fhircraft/fhir/resources/datatypes/utils.py
get_primitive_type_by_name
get_primitive_type_by_name(type_name: str) -> Union[TypeAliasType, None]
get_primitive_type_name
get_primitive_type_name(fhir_type: TypeAliasType) -> str
Get the string name of a FHIR primitive type.
Source code in fhircraft/fhir/resources/datatypes/utils.py
is_base64binary
is_boolean
is_canonical
is_code
is_date
Check if value is a valid FHIR Date.
Source code in fhircraft/fhir/resources/datatypes/utils.py
is_datetime
Check if value is a valid FHIR DateTime.
is_decimal
is_fhir_complex_type
is_fhir_complex_type(value: Any, fhir_type: FHIRBaseModel | type | str, raise_on_error: bool = True) -> bool
Check if a value conforms to a complex FHIR type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to check |
required |
fhir_type
|
FHIRBaseModel | type | str
|
The complex FHIR type (or name thereof) to check against |
required |
raise_on_error
|
bool
|
Whether to raise FHIRTypeError on unknown type (default: True) |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Raises:
| Type | Description |
|---|---|
FHIRTypeError
|
If the fhir_type is a string and does not correspond to a known complex type |
Source code in fhircraft/fhir/resources/datatypes/utils.py
is_fhir_primitive
Check if a value is a FHIR primitive type.
Source code in fhircraft/fhir/resources/datatypes/utils.py
is_fhir_primitive_type
is_fhir_primitive_type(value: Any, fhir_type: Type | TypeAliasType | str, raise_on_error: bool = True) -> bool
Check if a value conforms to a FHIR primitive type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to check |
required |
fhir_type
|
Type | TypeAliasType | str
|
The FHIR type to check against (class, TypeAliasType, or string name) |
required |
raise_on_error
|
bool
|
Whether to raise FHIRTypeError on unknown type (default: True) |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Raises:
| Type | Description |
|---|---|
FHIRTypeError
|
If the fhir_type is a string and does not correspond to a known type |
Examples:
>>> is_fhir_primitive_type("123", primitives.Integer)
True
>>> is_fhir_primitive_type("true", primitives.Boolean)
True
>>> is_fhir_primitive_type("invalid-date", primitives.Date)
False
>>> is_fhir_primitive_type(42, "UnsignedInt")
True
Source code in fhircraft/fhir/resources/datatypes/utils.py
is_fhir_resource_type
is_fhir_resource_type(value: Any, fhir_type: FHIRBaseModel | type | str, raise_on_error: bool = True) -> bool
Check if a value conforms to a FHIR resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to check |
required |
fhir_type
|
FHIRBaseModel | type | str
|
The complex FHIR type (or name thereof) to check against |
required |
raise_on_error
|
bool
|
Whether to raise FHIRTypeError on unknown type (default: True) |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Raises:
| Type | Description |
|---|---|
FHIRTypeError
|
If the fhir_type is a string and does not correspond to a known resource type |
Source code in fhircraft/fhir/resources/datatypes/utils.py
is_id
is_instant
Check if value is a valid FHIR Instant.
is_integer
is_integer64
is_markdown
is_oid
is_positive_int
is_string
is_time
Check if value is a valid FHIR Time.
is_unsigned_int
is_uri
is_url
is_uuid
list_primitive_types
List all available FHIR primitive type names.
Source code in fhircraft/fhir/resources/datatypes/utils.py
to_boolean
Convert value to FHIR Boolean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Value to convert |
required |
Returns:
| Type | Description |
|---|---|
Union[bool, None]
|
bool or None: Converted boolean value or None if conversion fails |
Examples:
Source code in fhircraft/fhir/resources/datatypes/utils.py
to_date
Convert value to FHIR Date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Value to convert |
required |
Returns:
| Type | Description |
|---|---|
Union[str, None]
|
str or None: Converted date string or None if conversion fails |
Source code in fhircraft/fhir/resources/datatypes/utils.py
to_datetime
Convert value to FHIR DateTime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Value to convert |
required |
Returns:
| Type | Description |
|---|---|
Union[str, None]
|
str or None: Converted datetime string or None if conversion fails |
Source code in fhircraft/fhir/resources/datatypes/utils.py
to_decimal
Convert value to FHIR Decimal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Value to convert |
required |
Returns:
| Type | Description |
|---|---|
Union[float, None]
|
float or None: Converted decimal value or None if conversion fails |
Source code in fhircraft/fhir/resources/datatypes/utils.py
to_integer
Convert value to FHIR Integer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Value to convert |
required |
Returns:
| Type | Description |
|---|---|
Union[int, None]
|
int or None: Converted integer value or None if conversion fails |
Source code in fhircraft/fhir/resources/datatypes/utils.py
to_quantity
Convert value to FHIR Quantity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Value to convert |
required |
Returns:
| Type | Description |
|---|---|
Union[Any, None]
|
Quantity or None: Converted Quantity object or None if conversion fails |
Source code in fhircraft/fhir/resources/datatypes/utils.py
to_string
Convert value to string representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Value to convert |
required |
Returns:
| Type | Description |
|---|---|
Union[str, None]
|
str or None: String representation or None if conversion fails |
Source code in fhircraft/fhir/resources/datatypes/utils.py
to_time
Convert value to FHIR Time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Value to convert |
required |
Returns:
| Type | Description |
|---|---|
Union[str, None]
|
str or None: Converted time string or None if conversion fails |