Skip to content

Commit

Permalink
✨ Core configurations for types Id & String are coming from c…
Browse files Browse the repository at this point in the history
…onstraint module.

* Default maximum char length ``Id`` is now 255.
* tests coverages are updated.
  • Loading branch information
nazrulworld committed Jul 29, 2024
1 parent 6d3e9cf commit a3536c3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
4 changes: 3 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ History
0.1.0b6 (unreleased)
--------------------

- Nothing changed yet.
- Core configurations for types ``Id`` & ``String`` are coming from constraint module.

- Default maximum char length ``Id`` is now 255.


0.1.0b5 (2024-07-28)
Expand Down
5 changes: 5 additions & 0 deletions fhir_core/constraints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__author__ = "Md Nazrul Islam"
__email__ = "[email protected]"

TYPES_ID_MAX_LENGTH = 255
TYPES_STRING_ALLOW_EMPTY_STR = False
17 changes: 12 additions & 5 deletions fhir_core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pydantic_core.core_schema import ValidationInfo
from typing_extensions import Annotated

from .constraints import TYPES_ID_MAX_LENGTH, TYPES_STRING_ALLOW_EMPTY_STR
from .fhirabstractmodel import FHIRAbstractModel

__author__ = "Md Nazrul Islam"
Expand Down Expand Up @@ -330,7 +331,7 @@ class StringModel(BaseModel):
```
"""

allow_empty_str: bool = False
allow_empty_str = TYPES_STRING_ALLOW_EMPTY_STR
__visit_name__ = "string"

def __iter__(self) -> typing.Iterator[BaseMetadata]:
Expand All @@ -357,6 +358,10 @@ def to_string(value):
assert isinstance(value, bytes)
return value.decode()

def __hash__(self) -> int:
""" """
return hash(self.__class__)


@dataclasses.dataclass(frozen=True, **SLOTS)
class Code(GroupedMetadata):
Expand All @@ -381,6 +386,9 @@ def to_string(value):
assert isinstance(value, str)
return value

def __hash__(self) -> int:
return hash(self.__class__)


@dataclasses.dataclass(frozen=True, **SLOTS)
class Id(GroupedMetadata):
Expand All @@ -390,8 +398,7 @@ class Id(GroupedMetadata):
(This might be an integer, an un-prefixed OID, UUID or any other identifier
pattern that meets these constraints.)
But it is possible to change the default behaviour by using configure_constraints()
method!
But it is possible to change the default behaviour by patching constraint.TYPES_ID_MAX_LENGTH value!
There are a lots of discussion about ``Resource.Id`` length of value.
1. https://bit.ly/360HksL
Expand All @@ -402,7 +409,7 @@ class Id(GroupedMetadata):

pattern = r"^[A-Za-z0-9\-.]+$"
min_length = 1
max_length = 64
max_length = TYPES_ID_MAX_LENGTH
__visit_name__ = "id"

def __iter__(self) -> typing.Iterator[BaseMetadata]:
Expand Down Expand Up @@ -966,7 +973,7 @@ def to_string(cls, value):
FHIR_PRIMITIVES_MAPS[BooleanType] = "boolean"

# string
StringType = Annotated[str, String(allow_empty_str=False)]
StringType = Annotated[str, String()]
FHIR_PRIMITIVES_MAPS[StringType] = "string"

# base64Binary
Expand Down
9 changes: 8 additions & 1 deletion tests/test_types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import decimal
import importlib
import typing
import uuid

Expand Down Expand Up @@ -46,15 +47,21 @@ class MySimpleStringModel(BaseModel):
MySimpleStringModel(name="")

assert exc_info.type is ValidationError
from fhir_core import constraints

StringType = Annotated[str, fhirtypes.String(allow_empty_str=True)]
constraints.TYPES_STRING_ALLOW_EMPTY_STR = True
importlib.reload(fhirtypes)
StringType = Annotated[str, fhirtypes.String()]

class MySimpleStringModel2(BaseModel):
name: StringType = Field(..., alias="name", title="My Name")

model = MySimpleStringModel2(name="")
assert model.name == ""

constraints.TYPES_STRING_ALLOW_EMPTY_STR = False
importlib.reload(fhirtypes)

class MySimpleStringModel3(BaseModel):
names: typing.List[fhirtypes.StringType] = Field(
..., alias="names", title="My Name"
Expand Down

0 comments on commit a3536c3

Please sign in to comment.