Skip to content

Commit

Permalink
Added phone validato
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed May 15, 2019
1 parent a87b47f commit f13d2bd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
31 changes: 29 additions & 2 deletions masonite/validation/Validator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from masonite.helpers import Dot as DictDot
from .RuleEnclosure import RuleEnclosure
import inspect
import re


class BaseValidation:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:

Expand Down
15 changes: 14 additions & 1 deletion tests/validation/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit f13d2bd

Please sign in to comment.