Skip to content

Commit

Permalink
Add Email type (#870)
Browse files Browse the repository at this point in the history
  • Loading branch information
eitanturok committed May 8, 2024
1 parent 4f8433d commit a84d78c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/reference/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Outlines provides custom Pydantic types so you can focus on your use case rather
| | numeric code | `outlines.types.countries.Numeric` | Valid [country numeric codes][wiki-country-numeric] |
| | name | `outlines.types.countries.Name` | Valid country names |
| | flag | `outlines.types.countries.Flag` | Valid flag emojis |
| | email | `outlines.types.Email` | Valid email address |

Some types require localization. We currently only support US types, but please don't hesitate to create localized versions of the different types and open a Pull Request. Localized types are specified using `types.locale` in the following way:

Expand Down
1 change: 1 addition & 0 deletions outlines/types/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from . import airports, countries
from .email import Email
from .isbn import ISBN
from .locales import locale
11 changes: 11 additions & 0 deletions outlines/types/email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""Email Address types."""
from pydantic import WithJsonSchema
from typing_extensions import Annotated

# Taken from StackOverflow
# https://stackoverflow.com/a/201378/14773537
EMAIL_REGEX = r"""(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])"""
Email = Annotated[
str,
WithJsonSchema({"type": "string", "pattern": EMAIL_REGEX}),
]
6 changes: 6 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
(types.ISBN, "9780596520687", True),
(types.ISBN, "ISBN-10: 0-596-52068-9", True),
(types.ISBN, "0-596-52068-9", True),
(types.Email, "eitan@gmail.com", True),
(types.Email, "99@yahoo.com", True),
(types.Email, "eitan@.gmail.com", False),
(types.Email, "myemail", False),
(types.Email, "eitan@gmail", False),
(types.Email, "eitan@my.custom.domain", True),
],
)
def test_type_regex(custom_type, test_string, should_match):
Expand Down

0 comments on commit a84d78c

Please sign in to comment.