Skip to content

Commit

Permalink
Merge pull request #7 from cholojuanito/dev
Browse files Browse the repository at this point in the history
Version 2.0 - initial null safety changes
  • Loading branch information
cholojuanito authored Jan 26, 2021
2 parents 14736ce + f26b8b0 commit f3009ad
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 63 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## Up next
* "Validate" card holder name and postal codes

## [2.0.0-nullsafety.0]
* Initial null safety changes

## [1.2.0]
* Card type parameter is now **required** when validating security codes
* More cards supported:
Expand Down
8 changes: 4 additions & 4 deletions lib/card_number.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,18 @@ CCNumValidationResults validateCardNumber(String ccNumStr,
isLuhnValid = checkLuhnValidity(trimmedNumStr);
}

int maxCardLength = _ccNumLengths.containsKey(type) ? _ccNumLengths[type].reduce(max) : _DEFAULT_MAX_CARD_NUM_LENGTH;
int maxCardLength = _ccNumLengths.containsKey(type) ? _ccNumLengths[type]!.reduce(max) : _DEFAULT_MAX_CARD_NUM_LENGTH;
String failedMessage = _DEFAULT_FAIL_MESSAGE;

// Check if the card number length is viable.
// If it is then decide the potential validity of this card number
// The card number will be potentially valid if:
// The number is luhn valid OR the card number isn't complete yet
if (_ccNumLengths[type].contains(trimmedNumStr.length)) {
if (_ccNumLengths[type]!.contains(trimmedNumStr.length)) {
isPotentiallyValid = isLuhnValid || trimmedNumStr.length < maxCardLength;

if (isLuhnValid && isPotentiallyValid) {
failedMessage = null; // Not a failed validation
failedMessage = ''; // Not a failed validation
}

return CCNumValidationResults(
Expand All @@ -109,7 +109,7 @@ CCNumValidationResults validateCardNumber(String ccNumStr,

bool potentialForMoreDigits = trimmedNumStr.length < maxCardLength;
if (potentialForMoreDigits) {
failedMessage = null; // Not an failed validation since there could be more digits being typed in
failedMessage = ''; // Not an failed validation since there could be more digits being typed in
}
// Not a valid card but if the str passed in is 'incomplete' it is potentially valid
// Incomplete means that the str passed in isn't as long as the max allowed card length
Expand Down
18 changes: 9 additions & 9 deletions lib/expiration_date.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ String _DEFAULT_MONTH_FAIL_MESSAGE = 'Card has expired this year';

/// Validates the card's expiration date based on the standard that no credit cards
ValidationResults validateExpirationDate(String expDateStr) {
if (expDateStr == null || expDateStr.isEmpty) {
if (expDateStr.isEmpty) {
return ValidationResults(
isValid: false,
isPotentiallyValid: false,
Expand All @@ -21,7 +21,7 @@ ValidationResults validateExpirationDate(String expDateStr) {
}

List<String> monthAndYear = _parseDate(expDateStr);
if (monthAndYear == null) {
if (monthAndYear.isEmpty) {
return ValidationResults(
isValid: false,
isPotentiallyValid: false,
Expand Down Expand Up @@ -71,7 +71,7 @@ ValidationResults validateExpirationDate(String expDateStr) {
}

ExpYearValidationResults _validateExpYear(String expYearStr,
[int maxYearsInFuture]) {
[int? maxYearsInFuture]) {

if (nonNumberRegex.hasMatch(expYearStr)) {
return ExpYearValidationResults(
Expand Down Expand Up @@ -104,7 +104,7 @@ ExpYearValidationResults _validateExpYear(String expYearStr,
isValid: false,
isPotentiallyValid: firstTwoDigits == firstTwoDigitsCurrYear,
expiresThisYear: false,
message: firstTwoDigits != firstTwoDigitsCurrYear ? 'Expiration year is 3 digits long' : null,
message: firstTwoDigits != firstTwoDigitsCurrYear ? 'Expiration year is 3 digits long' : '',
);
}

Expand Down Expand Up @@ -138,7 +138,7 @@ ExpYearValidationResults _validateExpYear(String expYearStr,
}

if (isValid) {
failedMessage = null;
failedMessage = '';
}

return ExpYearValidationResults(
Expand Down Expand Up @@ -168,7 +168,7 @@ ExpMonthValidationResults _validateExpMonth(String expMonthStr) {
String failMessage = _DEFAULT_MONTH_FAIL_MESSAGE;

if (isValid && isValidForThisYear) {
failMessage = null;
failMessage = '';
}

return ExpMonthValidationResults(
Expand All @@ -194,14 +194,14 @@ List<String> _parseDate(String expDateStr) {
String formattedStr = expDateStr.replaceAll('-', '/')
..replaceAll(whiteSpaceRegex, '');

Match match = expDateFormat.firstMatch(formattedStr);
Match? match = expDateFormat.firstMatch(formattedStr);
if (match != null) {
print("matched! ${match[0]}");
} else {
return null;
return [];
}

List<String> monthAndYear = match[0].split('/');
List<String> monthAndYear = match[0]!.split('/');

return monthAndYear;
}
2 changes: 1 addition & 1 deletion lib/postal_code.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'validation_results.dart';
const int DEFAULT_MIN_POSTAL_CODE_LENGTH = 3;

/// Checks if the postal code
ValidationResults validatePostalCode(String postalCode, {int minLength}) {
ValidationResults validatePostalCode(String postalCode, {int? minLength}) {
// TODO implement
throw UnimplementedError();
}
30 changes: 15 additions & 15 deletions lib/validation_results.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class ValidationResults {
String message;

ValidationResults({
this.isValid,
this.isPotentiallyValid,
this.message,
required this.isValid,
required this.isPotentiallyValid,
this.message = '',
});
}

Expand All @@ -30,10 +30,10 @@ class CCNumValidationResults extends ValidationResults {
CreditCardType ccType;

CCNumValidationResults({
this.ccType,
bool isValid,
bool isPotentiallyValid,
String message,
required this.ccType,
required bool isValid,
required bool isPotentiallyValid,
String message = '',
}) : super(
isValid: isValid,
isPotentiallyValid: isPotentiallyValid,
Expand All @@ -46,10 +46,10 @@ class ExpYearValidationResults extends ValidationResults {
bool expiresThisYear;

ExpYearValidationResults({
this.expiresThisYear,
bool isValid,
bool isPotentiallyValid,
String message,
required this.expiresThisYear,
required bool isValid,
required bool isPotentiallyValid,
String message = '',
}) : super(
isValid: isValid,
isPotentiallyValid: isPotentiallyValid,
Expand All @@ -62,10 +62,10 @@ class ExpMonthValidationResults extends ValidationResults {
bool isValidForCurrentYear;

ExpMonthValidationResults({
this.isValidForCurrentYear,
bool isValid,
bool isPotentiallyValid,
String message,
required this.isValidForCurrentYear,
required bool isValid,
required bool isPotentiallyValid,
String message = '',
}) : super(
isValid: isValid,
isPotentiallyValid: isPotentiallyValid,
Expand Down
53 changes: 23 additions & 30 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.4.2"
version: "2.5.0-nullsafety.3"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
version: "2.1.0-nullsafety.3"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.3"
version: "1.2.0-nullsafety.3"
cli_util:
dependency: transitive
description:
Expand All @@ -56,7 +56,7 @@ packages:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.14.13"
version: "1.15.0-nullsafety.5"
convert:
dependency: transitive
description:
Expand All @@ -77,7 +77,7 @@ packages:
name: credit_card_type_detector
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "2.0.0-nullsafety.0"
crypto:
dependency: transitive
description:
Expand All @@ -99,13 +99,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
http:
dependency: transitive
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.2"
http_multi_server:
dependency: transitive
description:
Expand Down Expand Up @@ -140,7 +133,7 @@ packages:
name: js
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.2"
version: "0.6.3-nullsafety.3"
logging:
dependency: transitive
description:
Expand All @@ -154,14 +147,14 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.9"
version: "0.12.10-nullsafety.3"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.4"
version: "1.3.0-nullsafety.6"
mime:
dependency: transitive
description:
Expand Down Expand Up @@ -203,21 +196,21 @@ packages:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
version: "1.8.0-nullsafety.3"
pedantic:
dependency: transitive
description:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.9.2"
version: "1.10.0-nullsafety.3"
pool:
dependency: transitive
description:
name: pool
url: "https://pub.dartlang.org"
source: hosted
version: "1.4.0"
version: "1.5.0-nullsafety.3"
pub_semver:
dependency: transitive
description:
Expand Down Expand Up @@ -259,77 +252,77 @@ packages:
name: source_map_stack_trace
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
version: "2.1.0-nullsafety.4"
source_maps:
dependency: transitive
description:
name: source_maps
url: "https://pub.dartlang.org"
source: hosted
version: "0.10.9"
version: "0.10.10-nullsafety.3"
source_span:
dependency: transitive
description:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
version: "1.8.0-nullsafety.4"
stack_trace:
dependency: transitive
description:
name: stack_trace
url: "https://pub.dartlang.org"
source: hosted
version: "1.9.6"
version: "1.10.0-nullsafety.6"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
version: "2.1.0-nullsafety.3"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.5"
version: "1.1.0-nullsafety.3"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.2.0-nullsafety.3"
test:
dependency: "direct dev"
description:
name: test
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.7"
version: "1.16.0-nullsafety.16"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.18+1"
version: "0.2.19-nullsafety.6"
test_core:
dependency: transitive
description:
name: test_core
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.11+4"
version: "0.3.12-nullsafety.15"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.0-nullsafety.5"
vm_service:
dependency: transitive
description:
Expand Down Expand Up @@ -366,4 +359,4 @@ packages:
source: hosted
version: "2.2.1"
sdks:
dart: ">=2.9.0 <3.0.0"
dart: ">=2.12.0-133.7.beta <3.0.0"
Loading

0 comments on commit f3009ad

Please sign in to comment.