mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 09:26:08 -05:00
Use validation of recipients from utils
This was added to utils in 5914da74f1
This means that:
- we are doing the exact same validation in the API and admin app
- we are actually validating phone numbers for the correct format (hence all the
changes to the tests)
This commit is contained in:
@@ -6,10 +6,7 @@ from . import models
|
||||
from app.dao.permissions_dao import permission_dao
|
||||
from marshmallow import (post_load, ValidationError, validates, validates_schema)
|
||||
from marshmallow_sqlalchemy import field_for
|
||||
|
||||
mobile_regex = re.compile("^\\+44[\\d]{10}$")
|
||||
|
||||
email_regex = re.compile("(^[^@^\\s]+@[^@^\\.^\\s]+(\\.[^@^\\.^\\s]*)*\.(.+))")
|
||||
from utils.recipients import validate_email_address, InvalidEmailError, validate_phone_number, InvalidPhoneError
|
||||
|
||||
|
||||
# TODO I think marshmallow provides a better integration and error handling.
|
||||
@@ -128,8 +125,10 @@ class SmsNotificationSchema(NotificationSchema):
|
||||
|
||||
@validates('to')
|
||||
def validate_to(self, value):
|
||||
if not mobile_regex.match(value):
|
||||
raise ValidationError('Invalid phone number, must be of format +441234123123')
|
||||
try:
|
||||
validate_phone_number(value)
|
||||
except InvalidPhoneError as error:
|
||||
raise ValidationError('Invalid phone number: {}'.format(error))
|
||||
|
||||
|
||||
class EmailNotificationSchema(NotificationSchema):
|
||||
@@ -138,7 +137,9 @@ class EmailNotificationSchema(NotificationSchema):
|
||||
|
||||
@validates('to')
|
||||
def validate_to(self, value):
|
||||
if not email_regex.match(value):
|
||||
try:
|
||||
validate_email_address(value)
|
||||
except InvalidEmailError:
|
||||
raise ValidationError('Invalid email')
|
||||
|
||||
|
||||
@@ -174,7 +175,9 @@ class InvitedUserSchema(BaseSchema):
|
||||
|
||||
@validates('email_address')
|
||||
def validate_to(self, value):
|
||||
if not email_regex.match(value):
|
||||
try:
|
||||
validate_email_address(value)
|
||||
except InvalidEmailError:
|
||||
raise ValidationError('Invalid email')
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user