Skip to content

Commit

Permalink
added neews validators
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobaraujo7 committed Aug 24, 2024
1 parent 701b8d3 commit ddd387f
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ Here’s a complete list of available validators you can use:
- **isNotNull**: Checks if a value is not null.
- **isEmpty**: Checks if a string is empty.
- **validCPF**: Checks if a string is a valid CPF (for use in Brazil).
- **validCNPJ**: Checks if a string is a valid CNPJ (for use in Brazil).
- **validCEP**: Checks if a string is a valid CEP (for use in Brazil).
- **validCredCard**: Checks if a string is a valid Credit Card.

## Usage with Flutter

Expand Down
20 changes: 20 additions & 0 deletions lib/src/validators/valid_cep_validator.dart
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,
);
}
}
54 changes: 54 additions & 0 deletions lib/src/validators/valid_cnpj_validator.dart
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]);
}
}
45 changes: 45 additions & 0 deletions lib/src/validators/valid_creditcard_validator.dart
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);
}
}
3 changes: 3 additions & 0 deletions lib/src/validators/validators.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ part 'must_have_uppercase.dart';
part 'not_empty_validator.dart';
part 'not_equal_validator.dart';
part 'range_validator.dart';
part 'valid_cep_validator.dart';
part 'valid_cnpj_validator.dart';
part 'valid_cpf.dart';
part 'valid_creditcard_validator.dart';
part 'valid_email_validator.dart';
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: "A Dart/Flutter package for building strongly typed validation rule
repository: https://github.com/Flutterando/lucid_validation
homepage: https://pub.dev/documentation/lucid_validation/latest/

version: 0.0.1+1
version: 0.0.2

environment:
sdk: ">=3.0.0 <4.0.0"
Expand Down

0 comments on commit ddd387f

Please sign in to comment.