Skip to content

Commit

Permalink
add email validator
Browse files Browse the repository at this point in the history
  • Loading branch information
dni committed Apr 23, 2024
1 parent b5963c1 commit a8844f7
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lnurl/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
PositiveInt,
ValidationError,
parse_obj_as,
validator,
)
from pydantic.errors import UrlHostTldError, UrlSchemeError
from pydantic.networks import Parts
Expand Down Expand Up @@ -215,12 +216,14 @@ def __init__(self, address: str):
self.address = address
self.url = self.__get_url__(address)

@validator("address")
def is_valid_email_address(cls, email: str) -> bool:
email_regex = r"[A-Za-z0-9\._%+-]+@[A-Za-z0-9\.-]+\.[A-Za-z]{2,63}"
return re.fullmatch(email_regex, email) is not None

@classmethod
def __get_url__(cls, address: str) -> Union[OnionUrl, ClearnetUrl, DebugUrl]:
name_domain = address.split("@")
if len(name_domain) != 2 or len(name_domain[1].split(".")) < 2:
raise ValueError("Invalid Lightning address.")

name, domain = name_domain
url = ("http://" if domain.endswith(".onion") else "https://") + domain + "/.well-known/lnurlp/" + name
return parse_obj_as(Union[OnionUrl, ClearnetUrl, DebugUrl], url) # type: ignore
Expand Down

0 comments on commit a8844f7

Please sign in to comment.