Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,30 +111,40 @@ class Product(BaseModel):

@validator("*", pre=True)
@classmethod
def parse_empty_string_as_null(cls, v):
def _parse_empty_string_as_null(cls, v):
"""Safe measure: database entries are sometimes left blank instead of null"""
if isinstance(v, str) and len(v.strip()) == 0:
return None
return v

@validator("name", pre=True, always=True)
@classmethod
def validate_name(cls, v):
def _validate_name(cls, v):
if v not in FRONTEND_APPS_AVAILABLE:
msg = f"{v} is not in available front-end apps {FRONTEND_APPS_AVAILABLE}"
raise ValueError(msg)
return v

@validator("host_regex", pre=True)
@classmethod
def _strip_whitespaces(cls, v):
if v and isinstance(v, str):
# Prevents unintended leading & trailing spaces when added
# manually in the database
return v.strip()
return v

@property
def twilio_alpha_numeric_sender_id(self) -> str:
return self.short_name or self.display_name.replace(string.punctuation, "")[:11]

class Config:
alias_generator = snake_to_camel # to export
allow_population_by_field_name = True
anystr_strip_whitespace = True
extra = Extra.ignore
frozen = True # read-only
orm_mode = True
extra = Extra.ignore
schema_extra: ClassVar[dict[str, Any]] = {
"examples": [
{
Expand Down
19 changes: 19 additions & 0 deletions services/web/server/tests/unit/isolated/test_products_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,22 @@ def test_product_to_static():
],
"isPaymentEnabled": False,
}


def test_product_host_regex_with_spaces():
data = Product.Config.schema_extra["examples"][2]

# with leading and trailing spaces and uppercase (tests anystr_strip_whitespace )
data["support_email"] = " [email protected] "

# with leading trailing spaces (tests validator("host_regex", pre=True))
expected = r"([\.-]{0,1}osparc[\.-])".strip()
data["host_regex"] = expected + " "

# parsing should strip all whitespaces and normalize email
product = Product.parse_obj(data)

assert product.host_regex.pattern == expected
assert product.host_regex.search("osparc.bar.com")

assert product.support_email == "[email protected]"
Loading