Skip to content

Commit

Permalink
fix(authenticator): required phone number validator (aws-amplify#4106)
Browse files Browse the repository at this point in the history
* fix(authenticator):required phone number validator

* fix: typo in validator
  • Loading branch information
Jordan-Nelson authored Nov 10, 2023
1 parent 4619117 commit eb6e68b
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ mixin AuthenticatorPhoneFieldMixin<FieldType extends Enum,
return phoneNumber?.ensureStartsWith('+${state.dialCode.value}');
}

String displayPhoneNumber(String phoneNumber) {
String displayPhoneNumber(String? phoneNumber) {
phoneNumber = phoneNumber ?? '';
final prefix = '+${state.dialCode.value}';
if (phoneNumber.startsWith(prefix)) {
phoneNumber = phoneNumber.substring(prefix.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ mixin AuthenticatorUsernameField<FieldType extends Enum,
}
}

String displayPhoneNumber(String? phoneNumber) {
phoneNumber = phoneNumber ?? '';
final prefix = '+${state.dialCode.value}';
if (phoneNumber.startsWith(prefix)) {
phoneNumber = phoneNumber.substring(prefix.length);
}
return phoneNumber;
}

@override
FormFieldValidator<UsernameInput> get validator {
switch (selectedUsernameType) {
Expand All @@ -183,7 +192,7 @@ mixin AuthenticatorUsernameField<FieldType extends Enum,
isOptional: isOptional,
context: context,
inputResolver: stringResolver.inputs,
)(input?.username);
)(displayPhoneNumber(input?.username));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import 'package:flutter/material.dart';
*/
final usernameRegex = RegExp(r'^\S+$');
final emailRegex = RegExp(r'^\S+@\S+$');
final phoneNumberRegex = RegExp(r'^\+\d+$');

/// Regex for a valid phone number.
///
/// The "+" prefix is excluded since validation is performed against the number
/// without the prefix. That is, if the full number is "+1-123-555-7890", the
/// "+1" is dropped prior to validation.
final phoneNumberRegex = RegExp(r'^\d+$');
final _codeRegex = RegExp(r'^\d{6}$');
final _uppercase = RegExp(r'[A-Z]');
final _lowercase = RegExp(r'[a-z]');
Expand Down Expand Up @@ -156,8 +162,8 @@ FormFieldValidator<String> validatePhoneNumber({
InputResolverKey.phoneNumberEmpty,
);
}
phoneNumber = phoneNumber.trim();
if (!phoneNumberRegex.hasMatch(phoneNumber)) {
final formattedNumber = phoneNumber.trim();
if (!phoneNumberRegex.hasMatch(formattedNumber)) {
return inputResolver.resolve(context, InputResolverKey.phoneNumberFormat);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ class _ConfirmSignInPhoneFieldState extends _ConfirmSignInTextFieldState
@override
FormFieldValidator<String> get validator {
return (phoneNumber) {
phoneNumber = formatPhoneNumber(phoneNumber);
phoneNumber = displayPhoneNumber(phoneNumber);
return validatePhoneNumber(
inputResolver: stringResolver.inputs,
context: context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ class _AuthenticatorPhoneFieldState<FieldType extends Enum>
@override
FormFieldValidator<String> get validator {
return (String? phoneNumber) {
phoneNumber = formatPhoneNumber(phoneNumber);
final validator = widget.validator;
if (validator != null) {
phoneNumber = formatPhoneNumber(phoneNumber);
return validator(phoneNumber);
}
phoneNumber = displayPhoneNumber(phoneNumber);
return validatePhoneNumber(
inputResolver: stringResolver.inputs,
context: context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ class _SignUpPhoneFieldState extends _SignUpTextFieldState
@override
FormFieldValidator<String> get validator {
return (phoneNumber) {
phoneNumber = formatPhoneNumber(phoneNumber);
phoneNumber = displayPhoneNumber(phoneNumber);
return validatePhoneNumber(
inputResolver: stringResolver.inputs,
context: context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,44 @@ void main() {
},
);

testWidgets(
'displays message when submitted with empty phone number if the field is required',
(tester) async {
await tester.pumpWidget(
MockAuthenticatorApp(
initialStep: AuthenticatorStep.signUp,
signUpForm: SignUpForm.custom(
fields: [
SignUpFormField.username(),
SignUpFormField.phoneNumber(required: true),
SignUpFormField.password(),
],
),
),
);
await tester.pumpAndSettle();

final signUpPage = SignUpPage(tester: tester);

await signUpPage.submitSignUp();

await tester.pumpAndSettle();

Finder findPhoneFieldError() => find.descendant(
of: signUpPage.phoneField,
matching: find.text('Phone Number field must not be blank.'),
);

expect(findPhoneFieldError(), findsOneWidget);

await signUpPage.enterPhoneNumber('1235556789');

await signUpPage.submitSignUp();

expect(findPhoneFieldError(), findsNothing);
},
);

testWidgets(
'displays message when submitted with invalid birth date',
(tester) async {
Expand Down

0 comments on commit eb6e68b

Please sign in to comment.