From f13d2bd881c9c7b36d98f23026486f20db49aa92 Mon Sep 17 00:00:00 2001 From: Joseph Mancuso Date: Tue, 14 May 2019 23:44:51 -0400 Subject: [PATCH] Added phone validato --- masonite/validation/Validator.py | 31 +++++++++++++++++++++++++++-- tests/validation/test_validation.py | 15 +++++++++++++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/masonite/validation/Validator.py b/masonite/validation/Validator.py index 56352b0d6..2f3a7f951 100644 --- a/masonite/validation/Validator.py +++ b/masonite/validation/Validator.py @@ -1,6 +1,7 @@ from masonite.helpers import Dot as DictDot from .RuleEnclosure import RuleEnclosure import inspect +import re class BaseValidation: @@ -250,8 +251,7 @@ def negated_message(self, attribute): class email(BaseValidation): - def passes(self, attribute, key, dictionary): - import re + def passes(self, attribute, key, dictionary): return re.compile(r"^[^.].+@([?)[a-zA-Z0-9-.])+.([a-zA-Z]{2,3}|[0-9]{1,3})(]?)$").match(attribute) def message(self, attribute): @@ -511,6 +511,33 @@ def message(self, attribute): def negated_message(self, attribute): return '{} must not be json'.format(attribute) +class phone(BaseValidation): + + def __init__(self, *rules, pattern="123-456-7890", messages={}, raises={}): + super().__init__(rules, messages={}, raises={}) + # 123-456-7890 + # (123)456-7890 + self.pattern = pattern + + def passes(self, attribute, key, dictionary): + if self.pattern == '(123)456-7890': + return re.compile(r"^\(\w{3}\)\w{3}\-\w{4}$").match(attribute) + elif self.pattern == '123-456-7890': + return re.compile(r"^\w{3}\-\w{3}\-\w{4}$").match(attribute) + + + def message(self, attribute): + if self.pattern == '(123)456-7890': + return '{} must be in the format (XXX)XXX-XXXX'.format(attribute) + elif self.pattern == '123-456-7890': + return '{} must be in the format XXX-XXX-XXXX'.format(attribute) + + + def negated_message(self, attribute): + if self.pattern == '(123)456-7890': + return '{} must not be in the format (XXX)XXX-XXXX'.format(attribute) + elif self.pattern == '123-456-7890': + return '{} must not be in the format XXX-XXX-XXXX'.format(attribute) class Validator: diff --git a/tests/validation/test_validation.py b/tests/validation/test_validation.py index 645dcff6f..ad9816cac 100644 --- a/tests/validation/test_validation.py +++ b/tests/validation/test_validation.py @@ -11,7 +11,7 @@ after_today, before_today, contains, date, email, equals, exists, greater_than, in_range, ip, is_future, - is_in, is_past, isnt, timezone) + is_in, is_past, isnt, phone, timezone) from masonite.validation.Validator import json as vjson from masonite.validation import RuleEnclosure from masonite.validation.Validator import (length, less_than, none, numeric, @@ -76,6 +76,19 @@ def test_active_domain(self): self.assertEqual(validate, {'domain1': ['domain1 must be an active domain name']}) + def test_phone(self): + validate = Validator().validate({ + 'phone': '876-182-1921' + }, phone('phone', pattern='123-456-7890')) + + self.assertEqual(len(validate), 0) + + validate = Validator().validate({ + 'phone': '(876)182-1921' + }, phone('phone', pattern='123-456-7890')) + + self.assertEqual(validate, {'phone': ['phone must be in the format XXX-XXX-XXXX']}) + def test_accepted(self): validate = Validator().validate({ 'terms': 'on'