Skip to content

Commit

Permalink
feat: improve contact form match pattern (#70)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
vsmalladi authored Apr 25, 2024
1 parent 7933b3d commit 17d44d7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
21 changes: 19 additions & 2 deletions compliance_suite/models/v1_0_0_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from enum import Enum
from typing import Dict, List, Optional

from pydantic import AnyUrl, BaseModel, Field
from pydantic import AnyUrl, BaseModel, Field, EmailStr, validator, ValidationError
from pydantic.tools import parse_obj_as


class TesCancelTaskResponse(BaseModel):
Expand Down Expand Up @@ -295,7 +296,7 @@ class Service(BaseModel):
organization: Organization = Field(
..., description='Organization providing the service'
)
contactUrl: Optional[AnyUrl] = Field(
contactUrl: Optional[str] = Field(
None,
description='URL of the contact for the provider of this service, e.g. a link to a contact form '
'(RFC 3986 format), or an email (RFC 2368 format).',
Expand Down Expand Up @@ -333,6 +334,22 @@ class Service(BaseModel):
example='1.0.0',
)

@validator('contactUrl')
def check_url_or_email(cls, value):
if value.startswith("mailto:"):
email = value[len("mailto:"):]
try:
EmailStr.validate(email)
return value
except ValidationError:
raise ValueError("Invalid email address")
else:
try:
parse_obj_as(AnyUrl, value)
return value
except ValidationError:
raise ValueError("Invalid URL")


class TesServiceType(ServiceType):
artifact: Artifact = Field(..., example='tes')
Expand Down
21 changes: 19 additions & 2 deletions compliance_suite/models/v1_1_0_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from enum import Enum
from typing import Dict, List, Optional

from pydantic import AnyUrl, BaseModel, Field
from pydantic import AnyUrl, BaseModel, Field, EmailStr, validator, ValidationError
from pydantic.tools import parse_obj_as


class TesCancelTaskResponse(BaseModel):
Expand Down Expand Up @@ -341,7 +342,7 @@ class Service(BaseModel):
organization: Organization = Field(
..., description='Organization providing the service'
)
contactUrl: Optional[AnyUrl] = Field(
contactUrl: Optional[str] = Field(
None,
description='URL of the contact for the provider of this service, e.g. a link to a contact form '
'(RFC 3986 format), or an email (RFC 2368 format).',
Expand Down Expand Up @@ -379,6 +380,22 @@ class Service(BaseModel):
example='1.0.0',
)

@validator('contactUrl')
def check_url_or_email(cls, value):
if value.startswith("mailto:"):
email = value[len("mailto:"):]
try:
EmailStr.validate(email)
return value
except ValidationError:
raise ValueError("Invalid email address")
else:
try:
parse_obj_as(AnyUrl, value)
return value
except ValidationError:
raise ValueError("Invalid URL")


class TesServiceType(ServiceType):
artifact: Artifact = Field(..., example='tes')
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ pytest==7.1.2
colorlog==6.6.0
ga4gh-testbed-lib==0.2.0
jinja2==3.1.2
dotmap==1.3.30
dotmap==1.3.30
email-validator==2.1.1

0 comments on commit 17d44d7

Please sign in to comment.