-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
701b8d3
commit ddd387f
Showing
7 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
## 0.0.2 | ||
|
||
* Added valid CEP, CNPJ and CreditCard | ||
|
||
## 0.0.1+1 | ||
|
||
* Initial Release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
part of 'validators.dart'; | ||
|
||
extension ValidCEPValidator on LucidValidationBuilder<String> { | ||
/// Adds a validation rule that checks if the [String] is a valid CEP (Brazilian postal code). | ||
/// | ||
/// This method verifies that the CEP is in the correct format (#####-###) and consists | ||
/// of only numbers. | ||
/// | ||
/// [message] is the error message returned if the validation fails. Defaults to "Invalid CEP". | ||
/// [code] is an optional error code for translation purposes. | ||
/// | ||
/// Returns the [LucidValidationBuilder] to allow for method chaining. | ||
LucidValidationBuilder<String> validCEP({String message = 'Invalid CEP', String code = 'invalid_cep'}) { | ||
return registerRule( | ||
(value) => RegExp(r'^\d{5}-?\d{3}$').hasMatch(value), | ||
message, | ||
code, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
part of 'validators.dart'; | ||
|
||
extension ValidCNPJValidator on LucidValidationBuilder<String> { | ||
/// Adds a validation rule that checks if the [String] is a valid CNPJ number. | ||
/// | ||
/// The CNPJ is the national identifier for Brazilian companies. This method | ||
/// verifies the format and the validity of the CNPJ, ensuring it follows the correct | ||
/// algorithm for digit verification. | ||
/// | ||
/// [message] is the error message returned if the validation fails. Defaults to "Invalid CNPJ". | ||
/// [code] is an optional error code for translation purposes. | ||
/// | ||
/// Returns the [LucidValidationBuilder] to allow for method chaining. | ||
LucidValidationBuilder<String> validCNPJ({String message = 'Invalid CNPJ', String code = 'invalid_cnpj'}) { | ||
return registerRule( | ||
(value) => _validateCNPJ(value), | ||
message, | ||
code, | ||
); | ||
} | ||
|
||
bool _validateCNPJ(String cnpj) { | ||
// Remove non-numeric characters | ||
cnpj = cnpj.replaceAll(RegExp(r'[^0-9]'), ''); | ||
if (cnpj.length != 14) return false; | ||
|
||
// Check if all digits are the same | ||
if (RegExp(r'^(\d)\1*$').hasMatch(cnpj)) return false; | ||
|
||
// Calculate the first check digit | ||
int sum = 0; | ||
List<int> weight1 = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]; | ||
for (int i = 0; i < 12; i++) { | ||
sum += int.parse(cnpj[i]) * weight1[i]; | ||
} | ||
int remainder = (sum % 11); | ||
int firstCheckDigit = remainder < 2 ? 0 : 11 - remainder; | ||
|
||
// Check the first check digit | ||
if (firstCheckDigit != int.parse(cnpj[12])) return false; | ||
|
||
// Calculate the second check digit | ||
sum = 0; | ||
List<int> weight2 = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]; | ||
for (int i = 0; i < 13; i++) { | ||
sum += int.parse(cnpj[i]) * weight2[i]; | ||
} | ||
remainder = (sum % 11); | ||
int secondCheckDigit = remainder < 2 ? 0 : 11 - remainder; | ||
|
||
// Check the second check digit | ||
return secondCheckDigit == int.parse(cnpj[13]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
part of 'validators.dart'; | ||
|
||
extension ValidCreditCardValidator on LucidValidationBuilder<String> { | ||
/// Adds a validation rule that checks if the [String] is a valid credit card number. | ||
/// | ||
/// This method uses the Luhn algorithm to verify the validity of a credit card number. | ||
/// It checks the length of the number and ensures it passes the Luhn check. | ||
/// | ||
/// [message] is the error message returned if the validation fails. Defaults to "Invalid credit card number". | ||
/// [code] is an optional error code for translation purposes. | ||
/// | ||
/// Returns the [LucidValidationBuilder] to allow for method chaining. | ||
LucidValidationBuilder<String> validCreditCard({String message = 'Invalid credit card number', String code = 'invalid_credit_card'}) { | ||
return registerRule( | ||
(value) => _validateCreditCard(value), | ||
message, | ||
code, | ||
); | ||
} | ||
|
||
bool _validateCreditCard(String number) { | ||
// Remove non-numeric characters | ||
number = number.replaceAll(RegExp(r'[^0-9]'), ''); | ||
if (number.isEmpty || number.length < 13 || number.length > 19) return false; | ||
|
||
int sum = 0; | ||
bool alternate = false; | ||
|
||
for (int i = number.length - 1; i >= 0; i--) { | ||
int n = int.parse(number[i]); | ||
|
||
if (alternate) { | ||
n *= 2; | ||
if (n > 9) { | ||
n -= 9; | ||
} | ||
} | ||
|
||
sum += n; | ||
alternate = !alternate; | ||
} | ||
|
||
return (sum % 10 == 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters