Skip to content

Commit 17d44d7

Browse files
authored
feat: improve contact form match pattern (#70)
* Update schema to match pattern of contact form or email. Closes #69. * Add in missing imports. * Fix test for Anyurl. * Fix missing imports. * Compliance with flake8.
1 parent 7933b3d commit 17d44d7

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

compliance_suite/models/v1_0_0_specs.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
from enum import Enum
1010
from typing import Dict, List, Optional
1111

12-
from pydantic import AnyUrl, BaseModel, Field
12+
from pydantic import AnyUrl, BaseModel, Field, EmailStr, validator, ValidationError
13+
from pydantic.tools import parse_obj_as
1314

1415

1516
class TesCancelTaskResponse(BaseModel):
@@ -295,7 +296,7 @@ class Service(BaseModel):
295296
organization: Organization = Field(
296297
..., description='Organization providing the service'
297298
)
298-
contactUrl: Optional[AnyUrl] = Field(
299+
contactUrl: Optional[str] = Field(
299300
None,
300301
description='URL of the contact for the provider of this service, e.g. a link to a contact form '
301302
'(RFC 3986 format), or an email (RFC 2368 format).',
@@ -333,6 +334,22 @@ class Service(BaseModel):
333334
example='1.0.0',
334335
)
335336

337+
@validator('contactUrl')
338+
def check_url_or_email(cls, value):
339+
if value.startswith("mailto:"):
340+
email = value[len("mailto:"):]
341+
try:
342+
EmailStr.validate(email)
343+
return value
344+
except ValidationError:
345+
raise ValueError("Invalid email address")
346+
else:
347+
try:
348+
parse_obj_as(AnyUrl, value)
349+
return value
350+
except ValidationError:
351+
raise ValueError("Invalid URL")
352+
336353

337354
class TesServiceType(ServiceType):
338355
artifact: Artifact = Field(..., example='tes')

compliance_suite/models/v1_1_0_specs.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
from enum import Enum
1010
from typing import Dict, List, Optional
1111

12-
from pydantic import AnyUrl, BaseModel, Field
12+
from pydantic import AnyUrl, BaseModel, Field, EmailStr, validator, ValidationError
13+
from pydantic.tools import parse_obj_as
1314

1415

1516
class TesCancelTaskResponse(BaseModel):
@@ -341,7 +342,7 @@ class Service(BaseModel):
341342
organization: Organization = Field(
342343
..., description='Organization providing the service'
343344
)
344-
contactUrl: Optional[AnyUrl] = Field(
345+
contactUrl: Optional[str] = Field(
345346
None,
346347
description='URL of the contact for the provider of this service, e.g. a link to a contact form '
347348
'(RFC 3986 format), or an email (RFC 2368 format).',
@@ -379,6 +380,22 @@ class Service(BaseModel):
379380
example='1.0.0',
380381
)
381382

383+
@validator('contactUrl')
384+
def check_url_or_email(cls, value):
385+
if value.startswith("mailto:"):
386+
email = value[len("mailto:"):]
387+
try:
388+
EmailStr.validate(email)
389+
return value
390+
except ValidationError:
391+
raise ValueError("Invalid email address")
392+
else:
393+
try:
394+
parse_obj_as(AnyUrl, value)
395+
return value
396+
except ValidationError:
397+
raise ValueError("Invalid URL")
398+
382399

383400
class TesServiceType(ServiceType):
384401
artifact: Artifact = Field(..., example='tes')

requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ pytest==7.1.2
1010
colorlog==6.6.0
1111
ga4gh-testbed-lib==0.2.0
1212
jinja2==3.1.2
13-
dotmap==1.3.30
13+
dotmap==1.3.30
14+
email-validator==2.1.1

0 commit comments

Comments
 (0)