Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for missing phone number types #70

Merged
merged 3 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 121 additions & 53 deletions demo/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,59 +60,127 @@ class _MyHomePageState extends State<MyHomePage> {
},
),
const SizedBox(height: 12),
ListTile(
title: const Text('international'),
trailing: phoneNumber != null
? Text(phoneNumber.international)
: const Text('-'),
),
ListTile(
title: const Text('Formatted national'),
trailing: phoneNumber != null
? Text(phoneNumber.formatNsn())
: const Text('-'),
),
ListTile(
title: const Text('Iso code'),
trailing: phoneNumber != null
? Text(phoneNumber.isoCode.name)
: const Text('-'),
),
ListTile(
title: const Text('Country Dial Code'),
trailing: phoneNumber != null
? Text(phoneNumber.countryCode)
: const Text('-'),
),
ListTile(
title: const Text('Is Valid'),
trailing: phoneNumber != null
? Text(phoneNumber.isValid().toString())
: const Text('-'),
),
ListTile(
title: const Text('Is Valid Mobile'),
trailing: phoneNumber != null
? Text(phoneNumber
.isValid(type: PhoneNumberType.mobile)
.toString())
: const Text('-'),
),
ListTile(
title: const Text('Is Valid Fixed Line'),
trailing: phoneNumber != null
? Text(phoneNumber
.isValid(type: PhoneNumberType.fixedLine)
.toString())
: const Text('-'),
),
ListTile(
title: const Text('Is Valid Voip'),
trailing: phoneNumber != null
? Text(phoneNumber
.isValid(type: PhoneNumberType.voip)
.toString())
: const Text('-'),
Expanded(
child: CustomScrollView(
slivers: [
SliverList(
delegate: SliverChildListDelegate(
[
ListTile(
title: const Text('international'),
trailing: phoneNumber != null
? Text(phoneNumber.international)
: const Text('-'),
),
ListTile(
title: const Text('Formatted national'),
trailing: phoneNumber != null
? Text(phoneNumber.formatNsn())
: const Text('-'),
),
ListTile(
title: const Text('Iso code'),
trailing: phoneNumber != null
? Text(phoneNumber.isoCode.name)
: const Text('-'),
),
ListTile(
title: const Text('Country Dial Code'),
trailing: phoneNumber != null
? Text(phoneNumber.countryCode)
: const Text('-'),
),
ListTile(
title: const Text('Is Valid'),
trailing: phoneNumber != null
? Text(phoneNumber.isValid().toString())
: const Text('-'),
),
ListTile(
title: const Text('Is Valid Mobile'),
trailing: phoneNumber != null
? Text(phoneNumber
.isValid(type: PhoneNumberType.mobile)
.toString())
: const Text('-'),
),
ListTile(
title: const Text('Is Valid Fixed Line'),
trailing: phoneNumber != null
? Text(phoneNumber
.isValid(type: PhoneNumberType.fixedLine)
.toString())
: const Text('-'),
),
ListTile(
title: const Text('Is Valid Voip'),
trailing: phoneNumber != null
? Text(phoneNumber
.isValid(type: PhoneNumberType.voip)
.toString())
: const Text('-'),
),
ListTile(
title: const Text('Is Valid Toll-Free'),
trailing: phoneNumber != null
? Text(phoneNumber
.isValid(type: PhoneNumberType.tollFree)
.toString())
: const Text('-'),
),
ListTile(
title: const Text('Is Valid Premium Rate'),
trailing: phoneNumber != null
? Text(phoneNumber
.isValid(type: PhoneNumberType.premiumRate)
.toString())
: const Text('-'),
),
ListTile(
title: const Text('Is Valid Shared Cost'),
trailing: phoneNumber != null
? Text(phoneNumber
.isValid(type: PhoneNumberType.sharedCost)
.toString())
: const Text('-'),
),
ListTile(
title: const Text('Is Valid Personal Number'),
trailing: phoneNumber != null
? Text(phoneNumber
.isValid(type: PhoneNumberType.personalNumber)
.toString())
: const Text('-'),
),
ListTile(
title: const Text('Is Valid UAN'),
trailing: phoneNumber != null
? Text(phoneNumber
.isValid(type: PhoneNumberType.uan)
.toString())
: const Text('-'),
),
ListTile(
title: const Text('Is Valid Pager'),
trailing: phoneNumber != null
? Text(phoneNumber
.isValid(type: PhoneNumberType.pager)
.toString())
: const Text('-'),
),
ListTile(
title: const Text('Is Valid Voice Mail'),
trailing: phoneNumber != null
? Text(phoneNumber
.isValid(type: PhoneNumberType.voiceMail)
.toString())
: const Text('-'),
),
],
),
),
],
),
),
],
),
Expand Down
7 changes: 7 additions & 0 deletions lib/src/validation/phone_number_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@ enum PhoneNumberType {
mobile,
fixedLine,
voip,
tollFree,
premiumRate,
sharedCost,
personalNumber,
uan,
pager,
voiceMail,
}
66 changes: 58 additions & 8 deletions lib/src/validation/validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,22 @@ abstract class Validator {
patterns.addAll([
_getPatterns(patternMetadatas, PhoneNumberType.fixedLine),
_getPatterns(patternMetadatas, PhoneNumberType.mobile),
_getPatterns(patternMetadatas, PhoneNumberType.voip)
if (patternMetadatas.voip.isNotEmpty)
_getPatterns(patternMetadatas, PhoneNumberType.voip),
if (patternMetadatas.tollFree.isNotEmpty)
_getPatterns(patternMetadatas, PhoneNumberType.tollFree),
if (patternMetadatas.premiumRate.isNotEmpty)
_getPatterns(patternMetadatas, PhoneNumberType.premiumRate),
if (patternMetadatas.sharedCost.isNotEmpty)
_getPatterns(patternMetadatas, PhoneNumberType.sharedCost),
if (patternMetadatas.personalNumber.isNotEmpty)
_getPatterns(patternMetadatas, PhoneNumberType.personalNumber),
if (patternMetadatas.uan.isNotEmpty)
_getPatterns(patternMetadatas, PhoneNumberType.uan),
if (patternMetadatas.pager.isNotEmpty)
_getPatterns(patternMetadatas, PhoneNumberType.pager),
if (patternMetadatas.voiceMail.isNotEmpty)
_getPatterns(patternMetadatas, PhoneNumberType.voiceMail),
]);
} else {
patterns.add(_getPatterns(patternMetadatas, type));
Expand Down Expand Up @@ -72,13 +87,20 @@ abstract class Validator {
final lengths = _getLengths(lengthMetadatas, type);
return Set.from(lengths);
} else {
// if the type is not specified it can be either mobile, fixedLine or voip
// so we return a set containing both
final rulesFixed =
_getLengths(lengthMetadatas, PhoneNumberType.fixedLine);
final rulesMobile = _getLengths(lengthMetadatas, PhoneNumberType.mobile);
final rulesVoip = _getLengths(lengthMetadatas, PhoneNumberType.voip);
return {...rulesFixed, ...rulesMobile, ...rulesVoip};
// if the type is not specified it can be any of them
// so we return a set containing all their possible lengths
return {
..._getLengths(lengthMetadatas, PhoneNumberType.fixedLine),
..._getLengths(lengthMetadatas, PhoneNumberType.mobile),
..._getLengths(lengthMetadatas, PhoneNumberType.voip),
..._getLengths(lengthMetadatas, PhoneNumberType.tollFree),
..._getLengths(lengthMetadatas, PhoneNumberType.premiumRate),
..._getLengths(lengthMetadatas, PhoneNumberType.sharedCost),
..._getLengths(lengthMetadatas, PhoneNumberType.personalNumber),
..._getLengths(lengthMetadatas, PhoneNumberType.uan),
..._getLengths(lengthMetadatas, PhoneNumberType.pager),
..._getLengths(lengthMetadatas, PhoneNumberType.voiceMail),
};
}
}

Expand All @@ -93,6 +115,20 @@ abstract class Validator {
return lengthMetadatas.fixedLine;
case PhoneNumberType.voip:
return lengthMetadatas.voip;
case PhoneNumberType.tollFree:
return lengthMetadatas.tollFree;
case PhoneNumberType.premiumRate:
return lengthMetadatas.premiumRate;
case PhoneNumberType.sharedCost:
return lengthMetadatas.sharedCost;
case PhoneNumberType.personalNumber:
return lengthMetadatas.personalNumber;
case PhoneNumberType.uan:
return lengthMetadatas.uan;
case PhoneNumberType.pager:
return lengthMetadatas.pager;
case PhoneNumberType.voiceMail:
return lengthMetadatas.voiceMail;
default:
return lengthMetadatas.general;
}
Expand All @@ -109,6 +145,20 @@ abstract class Validator {
return patternMetadatas.fixedLine;
case PhoneNumberType.voip:
return patternMetadatas.voip;
case PhoneNumberType.tollFree:
return patternMetadatas.tollFree;
case PhoneNumberType.premiumRate:
return patternMetadatas.premiumRate;
case PhoneNumberType.sharedCost:
return patternMetadatas.sharedCost;
case PhoneNumberType.personalNumber:
return patternMetadatas.personalNumber;
case PhoneNumberType.uan:
return patternMetadatas.uan;
case PhoneNumberType.pager:
return patternMetadatas.pager;
case PhoneNumberType.voiceMail:
return patternMetadatas.voiceMail;
default:
return patternMetadatas.general;
}
Expand Down
Loading