Skip to content

Commit ffba5ae

Browse files
pcrespovmrnicegyu11
authored andcommitted
🎨 Enhances Product parsing to strip whitespaces in host_regex (ITISFoundation#6419)
1 parent 060c9cb commit ffba5ae

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

‎services/web/server/src/simcore_service_webserver/products/_model.py‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,30 +111,40 @@ class Product(BaseModel):
111111

112112
@validator("*", pre=True)
113113
@classmethod
114-
def parse_empty_string_as_null(cls, v):
114+
def _parse_empty_string_as_null(cls, v):
115115
"""Safe measure: database entries are sometimes left blank instead of null"""
116116
if isinstance(v, str) and len(v.strip()) == 0:
117117
return None
118118
return v
119119

120120
@validator("name", pre=True, always=True)
121121
@classmethod
122-
def validate_name(cls, v):
122+
def _validate_name(cls, v):
123123
if v not in FRONTEND_APPS_AVAILABLE:
124124
msg = f"{v} is not in available front-end apps {FRONTEND_APPS_AVAILABLE}"
125125
raise ValueError(msg)
126126
return v
127127

128+
@validator("host_regex", pre=True)
129+
@classmethod
130+
def _strip_whitespaces(cls, v):
131+
if v and isinstance(v, str):
132+
# Prevents unintended leading & trailing spaces when added
133+
# manually in the database
134+
return v.strip()
135+
return v
136+
128137
@property
129138
def twilio_alpha_numeric_sender_id(self) -> str:
130139
return self.short_name or self.display_name.replace(string.punctuation, "")[:11]
131140

132141
class Config:
133142
alias_generator = snake_to_camel # to export
134143
allow_population_by_field_name = True
144+
anystr_strip_whitespace = True
145+
extra = Extra.ignore
135146
frozen = True # read-only
136147
orm_mode = True
137-
extra = Extra.ignore
138148
schema_extra: ClassVar[dict[str, Any]] = {
139149
"examples": [
140150
{

‎services/web/server/tests/unit/isolated/test_products_model.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,22 @@ def test_product_to_static():
7676
],
7777
"isPaymentEnabled": False,
7878
}
79+
80+
81+
def test_product_host_regex_with_spaces():
82+
data = Product.Config.schema_extra["examples"][2]
83+
84+
# with leading and trailing spaces and uppercase (tests anystr_strip_whitespace )
85+
data["support_email"] = " [email protected] "
86+
87+
# with leading trailing spaces (tests validator("host_regex", pre=True))
88+
expected = r"([\.-]{0,1}osparc[\.-])".strip()
89+
data["host_regex"] = expected + " "
90+
91+
# parsing should strip all whitespaces and normalize email
92+
product = Product.parse_obj(data)
93+
94+
assert product.host_regex.pattern == expected
95+
assert product.host_regex.search("osparc.bar.com")
96+
97+
assert product.support_email == "[email protected]"

0 commit comments

Comments
 (0)