Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🛠️ Improve email parser #67

Merged
merged 3 commits into from
Sep 18, 2024
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
4 changes: 2 additions & 2 deletions harambe/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from pydantic import (
BaseModel,
Field,
NameEmail,
ValidationError,
create_model,
ConfigDict,
Expand All @@ -14,6 +13,7 @@
from harambe.errors import SchemaValidationError
from harambe.parser.type_currency import ParserTypeCurrency
from harambe.parser.type_date import ParserTypeDate
from harambe.parser.type_email import ParserTypeEmail
from harambe.parser.type_enum import ParserTypeEnum
from harambe.parser.type_number import ParserTypeNumber
from harambe.parser.type_phone_number import ParserTypePhoneNumber
Expand Down Expand Up @@ -85,7 +85,7 @@ def _get_field_types(base_url: str) -> dict[SchemaFieldType, Any]:
"float": ParserTypeNumber,
"double": ParserTypeNumber,
"currency": ParserTypeCurrency(),
"email": NameEmail,
"email": ParserTypeEmail,
"enum": ParserTypeEnum,
"array": list,
"object": dict[str, Any],
Expand Down
16 changes: 16 additions & 0 deletions harambe/parser/type_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import Annotated

from pydantic import BeforeValidator, validate_email


def _validate_email(value: str) -> str:
if isinstance(value, str):
value = value.strip().lower().removeprefix("mailto:")
return validate_email(value)[1]
return value


ParserTypeEmail = Annotated[
str,
BeforeValidator(_validate_email),
]
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "harambe-sdk"
version = "0.28.0"
version = "0.28.1"
description = "Data extraction SDK for Playwright 🐒🍌"
authors = ["awtkns <[email protected]>"]
readme = "README.md"
Expand Down
13 changes: 13 additions & 0 deletions test/parser/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,3 +565,16 @@ def test_config_allow_extra_fields() -> None:
output_data = validator.validate(data, base_url="http://example.com")

assert output_data == data


def test_dump_email() -> None:
schema = {
"email": {"type": "email"},
}

data = {"email": "[email protected]"}

validator = PydanticSchemaParser(schema)
output_data = validator.validate(data, base_url="http://example.com")
assert output_data == data
assert isinstance(output_data["email"], str)
37 changes: 37 additions & 0 deletions test/parser/test_type_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import pytest
from pydantic import ValidationError, BaseModel

from harambe.parser.type_email import ParserTypeEmail


class TestModel(BaseModel):
email: ParserTypeEmail


@pytest.mark.parametrize(
"email, expected",
[
(
"mailto:[email protected]",
"[email protected]",
),
("John Doe <[email protected]>", "[email protected]"),
("[email protected]", "[email protected]"),
("[email protected]", "[email protected]"),
("[email protected]", "[email protected]"),
("[email protected]", "[email protected]"),
("[email protected]", "[email protected]"),
],
)
def test_parser_success(email, expected):
model = TestModel(email=email)
assert model.email == expected


@pytest.mark.parametrize(
"invalid_email",
["invalid", "invalid@", "invalid@.", "[email protected]", "invalid@com.", 123, None],
)
def test_parser_fail(invalid_email):
with pytest.raises(ValidationError):
TestModel(email=invalid_email)
Loading