Update Alphabet validator to accept hyphens
This commit is contained in:
leigh-mil 2018-10-24 10:34:59 -04:00 committed by GitHub
commit 778c029877
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 26 deletions

View File

@ -3,7 +3,7 @@ from wtforms.fields import StringField, TextAreaField
from wtforms.validators import Email, Optional
from .forms import ValidatedForm
from .validators import Alphabet, PhoneNumber
from .validators import Name, PhoneNumber
class CCPOReviewForm(ValidatedForm):
@ -11,19 +11,13 @@ class CCPOReviewForm(ValidatedForm):
"Instructions or comments",
description="Provide instructions or notes for additional information that is necessary to approve the request here. The requestor may then re-submit the updated request or initiate contact outside of AT-AT if further discussion is required. <strong>This message will be shared with the person making the JEDI request.</strong>.",
)
fname_mao = StringField(
"First Name (optional)", validators=[Optional(), Alphabet()]
)
lname_mao = StringField("Last Name (optional)", validators=[Optional(), Alphabet()])
fname_mao = StringField("First Name (optional)", validators=[Optional(), Name()])
lname_mao = StringField("Last Name (optional)", validators=[Optional(), Name()])
email_mao = EmailField(
"Mission Owner e-mail (optional)", validators=[Optional(), Email()]
)
phone_mao = TelField(
"Mission Owner phone number (optional)", validators=[Optional(), PhoneNumber()]
)
fname_ccpo = StringField(
"First Name (optional)", validators=[Optional(), Alphabet()]
)
lname_ccpo = StringField(
"Last Name (optional)", validators=[Optional(), Alphabet()]
)
fname_ccpo = StringField("First Name (optional)", validators=[Optional(), Name()])
lname_ccpo = StringField("Last Name (optional)", validators=[Optional(), Name()])

View File

@ -8,11 +8,11 @@ from .fields import SelectField
from .forms import ValidatedForm
from .data import SERVICE_BRANCHES
from .validators import Alphabet, DateRange, PhoneNumber
from .validators import Name, DateRange, PhoneNumber
USER_FIELDS = {
"first_name": StringField("First Name", validators=[Alphabet()]),
"last_name": StringField("Last Name", validators=[Alphabet()]),
"first_name": StringField("First Name", validators=[Name()]),
"last_name": StringField("Last Name", validators=[Name()]),
"email": EmailField(
"E-mail Address",
description="Enter your preferred contact e-mail address",

View File

@ -50,13 +50,15 @@ def PhoneNumber(message="Please enter a valid 5 or 10 digit phone number."):
return _is_phone_number
def Alphabet(message="Please enter only letters."):
def _alphabet(form, field):
match = re.match(r"[A-Za-z]+", field.data)
def Name(
message="This field accepts letters, numbers, commas, apostrophes, hyphens, and periods."
):
def _name(form, field):
match = re.match(r"[\w \,\.\'\-]+", field.data)
if not match or match.group() != field.data:
raise ValidationError(message)
return _alphabet
return _name
def ListItemRequired(message="Please provide at least one.", empty_values=("", None)):

View File

@ -1,7 +1,7 @@
from wtforms.validators import ValidationError
import pytest
from atst.forms.validators import Alphabet, IsNumber, PhoneNumber, ListItemsUnique
from atst.forms.validators import Name, IsNumber, PhoneNumber, ListItemsUnique
class TestIsNumber:
@ -38,16 +38,19 @@ class TestPhoneNumber:
validator(dummy_form, dummy_field)
class TestAlphabet:
@pytest.mark.parametrize("valid", ["a", "abcde"])
def test_Alphabet_accepts_letters(self, valid, dummy_form, dummy_field):
validator = Alphabet()
class TestName:
@pytest.mark.parametrize("valid", ["a", "abcde", "hi mark", "cloud9", "niña"])
def test_Name_accepts_letters(self, valid, dummy_form, dummy_field):
validator = Name()
dummy_field.data = valid
validator(dummy_form, dummy_field)
@pytest.mark.parametrize("invalid", ["", "hi mark", "cloud9"])
def test_Alphabet_rejects_non_letters(self, invalid, dummy_form, dummy_field):
validator = Alphabet()
@pytest.mark.parametrize(
"invalid",
["", "/my name", ":-)", "Name&Name", "Ke$ha", "A^Name", "#yourvalidname"],
)
def test_Name_rejects_invalid_characters(self, invalid, dummy_form, dummy_field):
validator = Name()
dummy_field.data = invalid
with pytest.raises(ValidationError):
validator(dummy_form, dummy_field)