diff --git a/flask_validator/constraints/internet.py b/flask_validator/constraints/internet.py index 428ac34..739d4a4 100644 --- a/flask_validator/constraints/internet.py +++ b/flask_validator/constraints/internet.py @@ -25,6 +25,7 @@ class ValidateEmail(Validator): allow_empty_local (bool) Set to True to allow an empty local part (i.e. @example.com), e.g. for validating Postfix aliases. allow_null: (bool) Allow null values + blacklist_domain: (list) Domains to be excluded to be used (i.e. [temp-email.org]) throw_exception: (bool) Throw a ValidateError if the validation fails """ @@ -33,21 +34,28 @@ class ValidateEmail(Validator): check_deliverability = True allow_empty_local = False - def __init__(self, field, allow_smtputf8=True,check_deliverability=True, allow_empty_local=False, - allow_null=True, throw_exception=False, message=None): + def __init__(self, field, allow_smtputf8=True, check_deliverability=True, allow_empty_local=False, + allow_null=True, blacklist_domain=None, throw_exception=False, message=None): self.allow_smtputf8 = allow_smtputf8 self.check_deliverability = check_deliverability self.allow_empty_local = allow_empty_local + self.blacklist_domain = blacklist_domain Validator.__init__(self, field, allow_null, throw_exception, message) def check_value(self, value): try: - validate_email(value, - allow_smtputf8=self.allow_smtputf8, - check_deliverability=self.check_deliverability, - allow_empty_local=self.allow_empty_local) + mail_info = validate_email(value, + allow_smtputf8=self.allow_smtputf8, + check_deliverability=self.check_deliverability, + allow_empty_local=self.allow_empty_local) + + if self.blacklist_domain is not None \ + and len(self.blacklist_domain) > 0 \ + and mail_info.ascii_domain in self.blacklist_domain: + return False + return True except EmailNotValidError: return False diff --git a/flask_validator/constraints/others.py b/flask_validator/constraints/others.py index cbee223..ce500af 100644 --- a/flask_validator/constraints/others.py +++ b/flask_validator/constraints/others.py @@ -80,12 +80,23 @@ class ValidateISBN(Validator): Args: field: SQLAlchemy column to validate + isbn_type: ISBN_10, ISBN_13 or ANY allow_null: (bool) Allow null values throw_exception: (bool) Throw a ValidateError if the validation fails """ + def __init__(self, field, isbn_type="ANY", allow_null=True, throw_exception=False, message=None): + self.isbn_type = isbn_type + + Validator.__init__(self, field, allow_null, throw_exception, message) + def check_value(self, value): - return is_isbn10(value) or is_isbn13(value) + if self.isbn_type is None or "ANY": + return is_isbn10(value) or is_isbn13(value) + elif self.isbn_type == "ISBN_10": + return is_isbn10(value) + elif self.isbn_type == "ISBN_13": + return is_isbn13(value) class ValidateRange(Validator): diff --git a/test/test_constraint.py b/test/test_constraint.py index aec86ec..c27a192 100644 --- a/test/test_constraint.py +++ b/test/test_constraint.py @@ -121,7 +121,7 @@ def define_validators(self): ValidateString(self.DummyModel.string) ValidateBoolean(self.DummyModel.boolean) ValidateLength(self.DummyModel.string, 10, 2) - ValidateEmail(self.DummyModel.email) + self.validateEmail = ValidateEmail(self.DummyModel.email) ValidateRegex(self.DummyModel.regex, "[A-Z][a-z]+") ValidateIP(self.DummyModel.ip) ValidateIP(self.DummyModel.ipv6, True) @@ -135,7 +135,7 @@ def define_validators(self): ValidateCreditCard(self.DummyModel.creditcard) ValidateCurrency(self.DummyModel.currency) ValidateIBAN(self.DummyModel.iban) - ValidateISBN(self.DummyModel.isbn) + self.validateIBSN = ValidateISBN(self.DummyModel.isbn) ValidateBIC(self.DummyModel.bic) ValidateString(self.DummyModel.null, True) self.rangevalues = [11, 12, 13] @@ -231,6 +231,13 @@ def test_email(self): """ self.simple_validate('email', "test2@gmail.com", "not@") + self.validateEmail.throw_exception = True + self.validateEmail.blacklist_domain = ["fake.com"] + with self.assertRaises(ValidateError) as e: + self.dummy.email = "test@fake.com" + + self.assertEqual(str(e.exception), "Value test@fake.com from column email is not valid") + def test_regex(self): """ Testing Regex Validator @@ -315,6 +322,23 @@ def test_isbn(self): self.simple_validate('isbn', '978-3-16-148410-0', "111112") + self.validateIBSN.throw_exception = True + self.validateIBSN.isbn_type = 'ISBN_10' + with self.assertRaises(ValidateError) as e1: + self.dummy.isbn = '0-940016-73-7' + self.assertEqual(self.dummy.isbn, '0-940016-73-7') + self.dummy.isbn = 'Bad10' + + self.assertEqual(str(e1.exception), 'Value Bad10 from column isbn is not valid') + + self.validateIBSN.isbn_type = 'ISBN_13' + with self.assertRaises(ValidateError) as e1: + self.dummy.isbn = '978-3-16-148410-0' + self.assertEqual(self.dummy.isbn, '978-3-16-148410-0') + self.dummy.isbn = 'Bad13' + + self.assertEqual(str(e1.exception), 'Value Bad13 from column isbn is not valid') + def test_bic(self): """ Testing BIC @@ -351,10 +375,12 @@ def test_exception_with_message(self): """ default_value = self.dummy.str_exception - with self.assertRaises(ValidateError): + with self.assertRaises(ValidateError) as e: self.dummy.str_exception = 42 self.assertEqual(self.dummy.str_exception, default_value) + self.assertEqual(str(e.exception), "Test Message") + def test_range(self): """ Test Range