Add basic/generic email validator logic

for use mostly in non-web scenarios, probably
This commit is contained in:
Lance Edgar 2019-05-09 12:28:57 -05:00
parent a8db5db308
commit 684363bcde
2 changed files with 26 additions and 2 deletions

View file

@ -2,7 +2,7 @@
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar # Copyright © 2010-2019 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -73,6 +73,7 @@ requires = [
# (still, probably a better idea is to refactor so we can use 0.9) # (still, probably a better idea is to refactor so we can use 0.9)
'webhelpers2_grid==0.1', # 0.1 'webhelpers2_grid==0.1', # 0.1
'colander', # 1.7.0
'ColanderAlchemy', # 0.3.3 'ColanderAlchemy', # 0.3.3
'cornice', # 3.4.2 'cornice', # 3.4.2
'deform', # 2.0.4 'deform', # 2.0.4

View file

@ -2,7 +2,7 @@
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar # Copyright © 2010-2019 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -35,6 +35,7 @@ import humanize
from rattail.time import timezone, make_utc from rattail.time import timezone, make_utc
from rattail.files import resource_path from rattail.files import resource_path
import colander
from pyramid.renderers import get_renderer from pyramid.renderers import get_renderer
from webhelpers2.html import HTML, tags from webhelpers2.html import HTML, tags
@ -175,3 +176,25 @@ def get_effective_theme(rattail_config, theme=None, session=None):
raise ValueError("theme not available: {}".format(theme)) raise ValueError("theme not available: {}".format(theme))
return theme return theme
def validate_email_address(address):
"""
Perform basic validation on the given email address. This leverages the
``colander`` package for actual validation logic.
"""
node = colander.SchemaNode(typ=colander.String)
validator = colander.Email()
validator(node, address)
return address
def email_address_is_valid(address):
"""
Returns boolean indicating whether the address can validate.
"""
try:
validate_email_address(address)
except colander.Invalid:
return False
return True