Skip to content

Commit a3536c3

Browse files
committed
✨ Core configurations for types Id & String are coming from constraint module.
* Default maximum char length ``Id`` is now 255. * tests coverages are updated.
1 parent 6d3e9cf commit a3536c3

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

HISTORY.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ History
55
0.1.0b6 (unreleased)
66
--------------------
77

8-
- Nothing changed yet.
8+
- Core configurations for types ``Id`` & ``String`` are coming from constraint module.
9+
10+
- Default maximum char length ``Id`` is now 255.
911

1012

1113
0.1.0b5 (2024-07-28)

fhir_core/constraints.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
__author__ = "Md Nazrul Islam"
2+
__email__ = "email2nazrul@gmail.com"
3+
4+
TYPES_ID_MAX_LENGTH = 255
5+
TYPES_STRING_ALLOW_EMPTY_STR = False

fhir_core/types.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from pydantic_core.core_schema import ValidationInfo
2222
from typing_extensions import Annotated
2323

24+
from .constraints import TYPES_ID_MAX_LENGTH, TYPES_STRING_ALLOW_EMPTY_STR
2425
from .fhirabstractmodel import FHIRAbstractModel
2526

2627
__author__ = "Md Nazrul Islam"
@@ -330,7 +331,7 @@ class StringModel(BaseModel):
330331
```
331332
"""
332333

333-
allow_empty_str: bool = False
334+
allow_empty_str = TYPES_STRING_ALLOW_EMPTY_STR
334335
__visit_name__ = "string"
335336

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

361+
def __hash__(self) -> int:
362+
""" """
363+
return hash(self.__class__)
364+
360365

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

389+
def __hash__(self) -> int:
390+
return hash(self.__class__)
391+
384392

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

403410
pattern = r"^[A-Za-z0-9\-.]+$"
404411
min_length = 1
405-
max_length = 64
412+
max_length = TYPES_ID_MAX_LENGTH
406413
__visit_name__ = "id"
407414

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

968975
# string
969-
StringType = Annotated[str, String(allow_empty_str=False)]
976+
StringType = Annotated[str, String()]
970977
FHIR_PRIMITIVES_MAPS[StringType] = "string"
971978

972979
# base64Binary

tests/test_types.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import decimal
2+
import importlib
23
import typing
34
import uuid
45

@@ -46,15 +47,21 @@ class MySimpleStringModel(BaseModel):
4647
MySimpleStringModel(name="")
4748

4849
assert exc_info.type is ValidationError
50+
from fhir_core import constraints
4951

50-
StringType = Annotated[str, fhirtypes.String(allow_empty_str=True)]
52+
constraints.TYPES_STRING_ALLOW_EMPTY_STR = True
53+
importlib.reload(fhirtypes)
54+
StringType = Annotated[str, fhirtypes.String()]
5155

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

5559
model = MySimpleStringModel2(name="")
5660
assert model.name == ""
5761

62+
constraints.TYPES_STRING_ALLOW_EMPTY_STR = False
63+
importlib.reload(fhirtypes)
64+
5865
class MySimpleStringModel3(BaseModel):
5966
names: typing.List[fhirtypes.StringType] = Field(
6067
..., alias="names", title="My Name"

0 commit comments

Comments
 (0)