MasoniteFramework / core

Main pip package location for Masonite
http://masoniteproject.com
MIT License
85 stars 52 forks source link

Add a phone validator #701

Closed josephmancuso closed 5 years ago

josephmancuso commented 5 years ago

This is gonna be tricky because there are a lot of different formats for a phone number.

123-4567
123-456-7890
(631) 123-4567
1+(631) 123-4567

We're gonna have to do something like this maybe?

validate.phone('field', format='123-456-7890')

we can have predesignated formats that people can choose from and then we can do:

if format == '123-456-7890':
    # do some rule
elif format == '...'
   # other rule
else:
   raise Exception('Format not supported')
thetonus commented 5 years ago

I feel like this is a feature we could possibly take from another library. This problem has been solved many times before. Let's try to not reinvent the wheel. 😄

thetonus commented 5 years ago

https://github.com/daviddrysdale/python-phonenumbers

This might be a tad overkill, but I like what is going on here.

josephmancuso commented 5 years ago

hmm i thhink we can do it with simple regex. Not sure it's reinventing the wheel too much. That package looks awesome but I deff think it's overkill when we can just regex a string

thetonus commented 5 years ago

Oh, I just wanted to avoid a bunch of if statements checking all these conditions. lol

I was thinking about regex, but with international numbers and different number formats, I am not sure if we could come up with all the necessary ones. We could have a few defaults and have a way to pass a regex string if the dev needs a more complex one.

josephmancuso commented 5 years ago

thats actually a really good idea

josephmancuso commented 5 years ago
validate.phone('field', format='123-456-7890') # 2 or 3 most common defaults
validate.phone('field', pattern=r'\w{0,3}..') # regex
josephmancuso commented 5 years ago

actually wouldn't it just be better to add a regex validator in that case?

thetonus commented 5 years ago

hmm..good point.

We could just have a regex validator object, and pass that into the .phone method. We obviously could just validate the regex operator ourselves, or we can let the .phone method do it. You could pass in multiple regex validators (for different phone number representations), and the .phone method would figure it out.

josephmancuso commented 5 years ago

this is done for now. Can update it at a later point when new feature requests come in.