Skip to content
This repository was archived by the owner on Aug 30, 2021. It is now read-only.

Commit 0f4307c

Browse files
Add string length constraints (#15)
* Add string length constraints * Fix docs
1 parent 572682f commit 0f4307c

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

tests/test_vaccine_feed_ingest_schema_location.py

+10
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ def test_raises_on_invalid_location():
161161
),
162162
)
163163

164+
with pytest.raises(pydantic.error_wrappers.ValidationError):
165+
location.NormalizedLocation(
166+
id="source:" + "a" * 200,
167+
source=location.Source(
168+
source="source",
169+
id="id",
170+
data={"id": "id"},
171+
),
172+
)
173+
164174

165175
def test_valid_location():
166176
# Minimal record

vaccine_feed_ingest_schema/location.py

+26-12
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@
4242
# Source ids can have anything but a space or a colon. Those must be replaced with another character (like a dash).
4343
SOURCE_ID_RE = re.compile(r"^[^\s\:]+$")
4444

45+
# Max length for long text fields storing notes
46+
NOTE_MAX_LENGTH = 2046
47+
48+
# Max length for normal string value fields
49+
VALUE_MAX_LENGTH = 256
50+
51+
# Max length for short enum identifier fields
52+
ENUM_MAX_LENGTH = 64
53+
54+
# Max length for id string fields
55+
ID_MAX_LENGTH = 128
56+
4557

4658
class StringDatetime(datetime.datetime):
4759
@classmethod
@@ -211,9 +223,9 @@ class Address(BaseModel):
211223
},
212224
"""
213225

214-
street1: Optional[str]
215-
street2: Optional[str]
216-
city: Optional[str]
226+
street1: Optional[str] = Field(max_length=VALUE_MAX_LENGTH)
227+
street2: Optional[str] = Field(max_length=VALUE_MAX_LENGTH)
228+
city: Optional[str] = Field(max_length=VALUE_MAX_LENGTH)
217229
state: Optional[State]
218230
zip: Optional[str] = Field(regex=ZIPCODE_RE.pattern)
219231

@@ -245,7 +257,7 @@ class Contact(BaseModel):
245257
phone: Optional[str] = Field(regex=US_PHONE_RE.pattern)
246258
website: Optional[HttpUrl]
247259
email: Optional[EmailStr]
248-
other: Optional[str]
260+
other: Optional[str] = Field(max_length=NOTE_MAX_LENGTH)
249261

250262
@root_validator
251263
@classmethod
@@ -362,8 +374,10 @@ class Organization(BaseModel):
362374
"""
363375

364376
# Use VaccineProvider enum value if available overwise make your own.
365-
id: Union[VaccineProvider, str, None] = Field(regex=ENUM_VALUE_RE.pattern)
366-
name: Optional[str]
377+
id: Union[VaccineProvider, str, None] = Field(
378+
regex=ENUM_VALUE_RE.pattern, max_length=ENUM_MAX_LENGTH
379+
)
380+
name: Optional[str] = Field(max_length=VALUE_MAX_LENGTH)
367381

368382

369383
class Link(BaseModel):
@@ -377,9 +391,9 @@ class Link(BaseModel):
377391

378392
# Use LocationAuthority enum value if available, overwise make your own.
379393
authority: Union[LocationAuthority, VaccineProvider, str, None] = Field(
380-
regex=ENUM_VALUE_RE.pattern
394+
regex=ENUM_VALUE_RE.pattern, max_length=ENUM_MAX_LENGTH
381395
)
382-
id: Optional[str]
396+
id: Optional[str] = Field(regex=SOURCE_ID_RE.pattern, max_length=ID_MAX_LENGTH)
383397
uri: Optional[AnyUrl]
384398

385399

@@ -395,17 +409,17 @@ class Source(BaseModel):
395409
}
396410
"""
397411

398-
source: str = Field(regex=ENUM_VALUE_RE.pattern)
399-
id: str = Field(regex=SOURCE_ID_RE.pattern)
412+
source: str = Field(regex=ENUM_VALUE_RE.pattern, max_length=ENUM_MAX_LENGTH)
413+
id: str = Field(regex=SOURCE_ID_RE.pattern, max_length=ID_MAX_LENGTH)
400414
fetched_from_uri: Optional[AnyUrl]
401415
fetched_at: Optional[StringDatetime]
402416
published_at: Optional[StringDatetime]
403417
data: dict
404418

405419

406420
class NormalizedLocation(BaseModel):
407-
id: str = Field(regex=LOCATION_ID_RE.pattern)
408-
name: Optional[str]
421+
id: str = Field(regex=LOCATION_ID_RE.pattern, max_length=ID_MAX_LENGTH)
422+
name: Optional[str] = Field(max_length=VALUE_MAX_LENGTH)
409423
address: Optional[Address]
410424
location: Optional[LatLng]
411425
contact: Optional[List[Contact]]

0 commit comments

Comments
 (0)