Skip to content

Commit 672a7cf

Browse files
LorenzohidalgoDillon Nysdnys1Jordan-Nelson
authored
fix(authenticator): Username form validation Cognito requirements (#2296)
* updated validator * Update l10n files * Update packages/amplify_authenticator/lib/src/l10n/input_resolver.dart Co-authored-by: Dillon Nys <[email protected]> * chore: Reformat files * updated username temp regex expression * updated errorMaxLines Co-authored-by: Dillon Nys <[email protected]> Co-authored-by: Dillon Nys <[email protected]> Co-authored-by: Jordan Nelson <[email protected]>
1 parent 4c8fbed commit 672a7cf

12 files changed

+100
-43
lines changed

packages/amplify_authenticator/lib/src/l10n/generated/button_localizations.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ import 'package:intl/intl.dart' as intl;
2222

2323
import 'button_localizations_en.dart' deferred as button_localizations_en;
2424

25-
/// Callers can lookup localized strings with an instance of AuthenticatorButtonLocalizations returned
26-
/// by `AuthenticatorButtonLocalizations.of(context)`.
25+
/// Callers can lookup localized strings with an instance of AuthenticatorButtonLocalizations
26+
/// returned by `AuthenticatorButtonLocalizations.of(context)`.
2727
///
2828
/// Applications need to include `AuthenticatorButtonLocalizations.delegate()` in their app's
29-
/// localizationDelegates list, and the locales they support in the app's
30-
/// supportedLocales list. For example:
29+
/// `localizationDelegates` list, and the locales they support in the app's
30+
/// `supportedLocales` list. For example:
3131
///
32-
/// ```
32+
/// ```dart
3333
/// import 'generated/button_localizations.dart';
3434
///
3535
/// return MaterialApp(
@@ -44,14 +44,14 @@ import 'button_localizations_en.dart' deferred as button_localizations_en;
4444
/// Please make sure to update your pubspec.yaml to include the following
4545
/// packages:
4646
///
47-
/// ```
47+
/// ```yaml
4848
/// dependencies:
4949
/// # Internationalization support.
5050
/// flutter_localizations:
5151
/// sdk: flutter
5252
/// intl: any # Use the pinned version from flutter_localizations
5353
///
54-
/// # rest of dependencies
54+
/// # Rest of dependencies
5555
/// ```
5656
///
5757
/// ## iOS Applications

packages/amplify_authenticator/lib/src/l10n/generated/button_localizations_en.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import 'package:amplify_authenticator/amplify_authenticator.dart';
1717

1818
import 'package:intl/intl.dart' as intl;
19+
1920
import 'button_localizations.dart';
2021

2122
/// The translations for English (`en`).

packages/amplify_authenticator/lib/src/l10n/generated/country_localizations.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ import 'package:intl/intl.dart' as intl;
2121

2222
import 'country_localizations_en.dart' deferred as country_localizations_en;
2323

24-
/// Callers can lookup localized strings with an instance of AuthenticatorCountryLocalizations returned
25-
/// by `AuthenticatorCountryLocalizations.of(context)`.
24+
/// Callers can lookup localized strings with an instance of AuthenticatorCountryLocalizations
25+
/// returned by `AuthenticatorCountryLocalizations.of(context)`.
2626
///
2727
/// Applications need to include `AuthenticatorCountryLocalizations.delegate()` in their app's
28-
/// localizationDelegates list, and the locales they support in the app's
29-
/// supportedLocales list. For example:
28+
/// `localizationDelegates` list, and the locales they support in the app's
29+
/// `supportedLocales` list. For example:
3030
///
31-
/// ```
31+
/// ```dart
3232
/// import 'generated/country_localizations.dart';
3333
///
3434
/// return MaterialApp(
@@ -43,14 +43,14 @@ import 'country_localizations_en.dart' deferred as country_localizations_en;
4343
/// Please make sure to update your pubspec.yaml to include the following
4444
/// packages:
4545
///
46-
/// ```
46+
/// ```yaml
4747
/// dependencies:
4848
/// # Internationalization support.
4949
/// flutter_localizations:
5050
/// sdk: flutter
5151
/// intl: any # Use the pinned version from flutter_localizations
5252
///
53-
/// # rest of dependencies
53+
/// # Rest of dependencies
5454
/// ```
5555
///
5656
/// ## iOS Applications

packages/amplify_authenticator/lib/src/l10n/generated/input_localizations.dart

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ import 'package:intl/intl.dart' as intl;
2222

2323
import 'input_localizations_en.dart' deferred as input_localizations_en;
2424

25-
/// Callers can lookup localized strings with an instance of AuthenticatorInputLocalizations returned
26-
/// by `AuthenticatorInputLocalizations.of(context)`.
25+
/// Callers can lookup localized strings with an instance of AuthenticatorInputLocalizations
26+
/// returned by `AuthenticatorInputLocalizations.of(context)`.
2727
///
2828
/// Applications need to include `AuthenticatorInputLocalizations.delegate()` in their app's
29-
/// localizationDelegates list, and the locales they support in the app's
30-
/// supportedLocales list. For example:
29+
/// `localizationDelegates` list, and the locales they support in the app's
30+
/// `supportedLocales` list. For example:
3131
///
32-
/// ```
32+
/// ```dart
3333
/// import 'generated/input_localizations.dart';
3434
///
3535
/// return MaterialApp(
@@ -44,14 +44,14 @@ import 'input_localizations_en.dart' deferred as input_localizations_en;
4444
/// Please make sure to update your pubspec.yaml to include the following
4545
/// packages:
4646
///
47-
/// ```
47+
/// ```yaml
4848
/// dependencies:
4949
/// # Internationalization support.
5050
/// flutter_localizations:
5151
/// sdk: flutter
5252
/// intl: any # Use the pinned version from flutter_localizations
5353
///
54-
/// # rest of dependencies
54+
/// # Rest of dependencies
5555
/// ```
5656
///
5757
/// ## iOS Applications
@@ -240,6 +240,12 @@ abstract class AuthenticatorInputLocalizations {
240240
/// **'Confirm {attribute}'**
241241
String confirmAttribute(String attribute);
242242

243+
/// Warning for when username requirements are not met.
244+
///
245+
/// In en, this message translates to:
246+
/// **'Username must only contain alphanumeric characters and symbols.'**
247+
String get usernameRequirements;
248+
243249
/// Preamble to list of unment password requirements.
244250
///
245251
/// In en, this message translates to:
@@ -257,7 +263,7 @@ abstract class AuthenticatorInputLocalizations {
257263
///
258264
/// In en, this message translates to:
259265
/// **'at least {numCharacters, plural, =1{1{characterType} character} other{{numCharacters}{characterType} characters}}'**
260-
String passwordRequirementsAtLeast(int numCharacters, String characterType);
266+
String passwordRequirementsAtLeast(num numCharacters, String characterType);
261267

262268
/// Message for conflicting password and confirm password fields.
263269
///

packages/amplify_authenticator/lib/src/l10n/generated/input_localizations_en.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import 'package:amplify_authenticator/amplify_authenticator.dart';
1717

1818
import 'package:intl/intl.dart' as intl;
19+
1920
import 'input_localizations.dart';
2021

2122
/// The translations for English (`en`).
@@ -105,6 +106,10 @@ class AuthenticatorInputLocalizationsEn
105106
return 'Confirm $attribute';
106107
}
107108

109+
@override
110+
String get usernameRequirements =>
111+
'Username must only contain alphanumeric characters and symbols.';
112+
108113
@override
109114
String get passwordRequirementsPreamble => 'Password must include:';
110115

@@ -126,7 +131,7 @@ class AuthenticatorInputLocalizationsEn
126131
}
127132

128133
@override
129-
String passwordRequirementsAtLeast(int numCharacters, String characterType) {
134+
String passwordRequirementsAtLeast(num numCharacters, String characterType) {
130135
final String pluralString = intl.Intl.pluralLogic(
131136
numCharacters,
132137
locale: localeName,

packages/amplify_authenticator/lib/src/l10n/generated/message_localizations.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ import 'package:intl/intl.dart' as intl;
2121

2222
import 'message_localizations_en.dart' deferred as message_localizations_en;
2323

24-
/// Callers can lookup localized strings with an instance of AuthenticatorMessageLocalizations returned
25-
/// by `AuthenticatorMessageLocalizations.of(context)`.
24+
/// Callers can lookup localized strings with an instance of AuthenticatorMessageLocalizations
25+
/// returned by `AuthenticatorMessageLocalizations.of(context)`.
2626
///
2727
/// Applications need to include `AuthenticatorMessageLocalizations.delegate()` in their app's
28-
/// localizationDelegates list, and the locales they support in the app's
29-
/// supportedLocales list. For example:
28+
/// `localizationDelegates` list, and the locales they support in the app's
29+
/// `supportedLocales` list. For example:
3030
///
31-
/// ```
31+
/// ```dart
3232
/// import 'generated/message_localizations.dart';
3333
///
3434
/// return MaterialApp(
@@ -43,14 +43,14 @@ import 'message_localizations_en.dart' deferred as message_localizations_en;
4343
/// Please make sure to update your pubspec.yaml to include the following
4444
/// packages:
4545
///
46-
/// ```
46+
/// ```yaml
4747
/// dependencies:
4848
/// # Internationalization support.
4949
/// flutter_localizations:
5050
/// sdk: flutter
5151
/// intl: any # Use the pinned version from flutter_localizations
5252
///
53-
/// # rest of dependencies
53+
/// # Rest of dependencies
5454
/// ```
5555
///
5656
/// ## iOS Applications

packages/amplify_authenticator/lib/src/l10n/generated/title_localizations.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ import 'package:intl/intl.dart' as intl;
2121

2222
import 'title_localizations_en.dart' deferred as title_localizations_en;
2323

24-
/// Callers can lookup localized strings with an instance of AuthenticatorTitleLocalizations returned
25-
/// by `AuthenticatorTitleLocalizations.of(context)`.
24+
/// Callers can lookup localized strings with an instance of AuthenticatorTitleLocalizations
25+
/// returned by `AuthenticatorTitleLocalizations.of(context)`.
2626
///
2727
/// Applications need to include `AuthenticatorTitleLocalizations.delegate()` in their app's
28-
/// localizationDelegates list, and the locales they support in the app's
29-
/// supportedLocales list. For example:
28+
/// `localizationDelegates` list, and the locales they support in the app's
29+
/// `supportedLocales` list. For example:
3030
///
31-
/// ```
31+
/// ```dart
3232
/// import 'generated/title_localizations.dart';
3333
///
3434
/// return MaterialApp(
@@ -43,14 +43,14 @@ import 'title_localizations_en.dart' deferred as title_localizations_en;
4343
/// Please make sure to update your pubspec.yaml to include the following
4444
/// packages:
4545
///
46-
/// ```
46+
/// ```yaml
4747
/// dependencies:
4848
/// # Internationalization support.
4949
/// flutter_localizations:
5050
/// sdk: flutter
5151
/// intl: any # Use the pinned version from flutter_localizations
5252
///
53-
/// # rest of dependencies
53+
/// # Rest of dependencies
5454
/// ```
5555
///
5656
/// ## iOS Applications

packages/amplify_authenticator/lib/src/l10n/input_resolver.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ enum InputResolverKeyType {
5252
hint,
5353
confirmHint,
5454
empty,
55+
usernameRequirements,
5556
passwordRequirements,
5657
format,
5758
mismatch
@@ -80,6 +81,10 @@ class InputResolverKey {
8081
InputResolverKeyType.empty,
8182
field: InputField.username,
8283
);
84+
static const usernameRequirementsUnmet = InputResolverKey._(
85+
InputResolverKeyType.usernameRequirements,
86+
field: InputField.username,
87+
);
8388
static const passwordTitle = InputResolverKey._(
8489
InputResolverKeyType.title,
8590
field: InputField.password,
@@ -477,6 +482,11 @@ class InputResolver extends Resolver<InputResolverKey> {
477482
.warnInvalidFormat(title(context, field).toLowerCase());
478483
}
479484

485+
/// Returns the text displayed when the username requirements are not met
486+
String usernameRequires(BuildContext context) {
487+
return AuthenticatorLocalizations.inputsOf(context).usernameRequirements;
488+
}
489+
480490
/// Returns the text displayed when a password input does match the password requirements
481491
/// defined in the amplify configuration.
482492
String passwordRequires(
@@ -523,6 +533,8 @@ class InputResolver extends Resolver<InputResolverKey> {
523533
return confirmHint(context, key.field);
524534
case InputResolverKeyType.empty:
525535
return empty(context, key.field);
536+
case InputResolverKeyType.usernameRequirements:
537+
return usernameRequires(context);
526538
case InputResolverKeyType.passwordRequirements:
527539
return passwordRequires(context, key.unmetPasswordRequirements!);
528540
case InputResolverKeyType.mismatch:

packages/amplify_authenticator/lib/src/l10n/src/inputs/inputs_en.arb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@
146146
"description": "The field which can be filled."
147147
}
148148
}
149+
},
150+
"usernameRequirements": "Username must only contain alphanumeric characters and symbols.",
151+
"@usernameRequirements": {
152+
"description": "Warning for when username requirements are not met."
149153
},
150154
"passwordRequirementsPreamble": "Password must include:",
151155
"@passwordRequirementsPreamble": {

packages/amplify_authenticator/lib/src/mixins/authenticator_username_field.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,10 @@ mixin AuthenticatorUsernameField<FieldType,
179179
FormFieldValidator<UsernameInput> get validator {
180180
switch (selectedUsernameType) {
181181
case UsernameType.username:
182-
return (input) => simpleValidator(
183-
stringResolver.inputs.resolve(
184-
context,
185-
InputResolverKey.usernameEmpty,
186-
),
182+
return (input) => usernameValidator(
187183
isOptional: isOptional,
184+
context: context,
185+
inputResolver: stringResolver.inputs,
188186
)(input?.username);
189187
case UsernameType.email:
190188
return (input) => validateEmail(

0 commit comments

Comments
 (0)