Skip to content

Commit

Permalink
fix validations
Browse files Browse the repository at this point in the history
  • Loading branch information
wellgenio committed Aug 25, 2024
1 parent 8e29b1c commit 40aa858
Show file tree
Hide file tree
Showing 24 changed files with 125 additions and 83 deletions.
2 changes: 1 addition & 1 deletion example/lib/domain/dtos/login_param_dto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ class LoginParamDto {

setEmail(String value) => email = value;

setPassword(String value) => email = value;
setPassword(String value) => password = value;
}
6 changes: 4 additions & 2 deletions lib/src/validations/equal_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ extension EqualValidation<T, E> on LucidValidationBuilder<T, E> {
///
/// Example:
/// ```dart
/// final builder = LucidValidationBuilder<String>(key: 'confirmPassword');
/// builder.equalTo('password123');
/// ...
/// ruleFor((user) => user.confirmPassword, key: 'confirmPassword')
/// .equalTo((user) => user.password);
///
/// ```
LucidValidationBuilder<T, E> equalTo(T Function(E entity) predicate, {String message = r'Must be equal', String code = 'equal_error'}) {
return mustWith(
Expand Down
9 changes: 5 additions & 4 deletions lib/src/validations/greater_than_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ part of 'validations.dart';
///
/// This extension adds a `greaterThan` method that can be used to ensure that a number
/// is greater than a specified value.
extension GreaterThanValidation on LucidValidationBuilder<num, dynamic> {
extension GreaterThanValidation on SimpleValidationBuilder<num> {
/// Adds a validation rule that checks if the [num] is greater than [minValue].
///
/// [minValue] is the value that the number must be greater than.
Expand All @@ -15,10 +15,11 @@ extension GreaterThanValidation on LucidValidationBuilder<num, dynamic> {
///
/// Example:
/// ```dart
/// final builder = LucidValidationBuilder<num>(key: 'age');
/// builder.greaterThan(18);
/// ...
/// ruleFor((user) => user.age, key: 'age')
/// .greaterThan(18);
/// ```
LucidValidationBuilder<num, dynamic> greaterThan(num minValue, {String message = r'Must be greater than $minValue', String code = 'greater_than'}) {
SimpleValidationBuilder<num> greaterThan(num minValue, {String message = r'Must be greater than $minValue', String code = 'greater_than'}) {
return must(
(value) => value > minValue,
message.replaceAll('$minValue', minValue.toString()),
Expand Down
9 changes: 5 additions & 4 deletions lib/src/validations/is_empty_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ part of 'validations.dart';
///
/// This extension adds an `isEmpty` method that can be used to ensure that a string
/// is empty.
extension IsEmptyValidation on LucidValidationBuilder<String, dynamic> {
extension IsEmptyValidation on SimpleValidationBuilder<String> {
/// Adds a validation rule that checks if the [String] is empty.
///
/// [message] is the error message returned if the validation fails. Defaults to "Must be empty".
Expand All @@ -14,10 +14,11 @@ extension IsEmptyValidation on LucidValidationBuilder<String, dynamic> {
///
/// Example:
/// ```dart
/// final builder = LucidValidationBuilder<String>(key: 'field');
/// builder.isEmpty();
/// ...
/// ruleFor((user) => user.name, key: 'name')
/// .isEmpty();
/// ```
LucidValidationBuilder<String, dynamic> isEmpty({String message = 'Must be empty', String code = 'must_be_empty'}) {
SimpleValidationBuilder<String> isEmpty({String message = 'Must be empty', String code = 'must_be_empty'}) {
return must(
(value) => value.isEmpty,
message,
Expand Down
9 changes: 5 additions & 4 deletions lib/src/validations/is_not_null_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ part of 'validations.dart';
///
/// This extension adds an `isNotNull` method that can be used to ensure that a value
/// is not null.
extension IsNotNullValidation<T, E> on LucidValidationBuilder<T?, E> {
extension IsNotNullValidation<T> on SimpleValidationBuilder<T> {
/// Adds a validation rule that checks if the value is not null.
///
/// [message] is the error message returned if the validation fails. Defaults to "Cannot be null".
Expand All @@ -14,10 +14,11 @@ extension IsNotNullValidation<T, E> on LucidValidationBuilder<T?, E> {
///
/// Example:
/// ```dart
/// final builder = LucidValidationBuilder<String?>(key: 'requiredField');
/// builder.isNotNull();
/// ...
/// ruleFor((user) => user.name, key: 'name') // required field
/// .isNotNull();
/// ```
LucidValidationBuilder<T?, E> isNotNull({String message = 'Cannot be null', String code = 'cannot_be_null'}) {
SimpleValidationBuilder<T> isNotNull({String message = 'Cannot be null', String code = 'cannot_be_null'}) {
return must(
(value) => value != null,
message,
Expand Down
9 changes: 5 additions & 4 deletions lib/src/validations/is_null_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ part of 'validations.dart';
///
/// This extension adds an `isNull` method that can be used to ensure that a value
/// is null.
extension IsNullValidation<T, E> on LucidValidationBuilder<T?, E> {
extension IsNullValidation<T> on SimpleValidationBuilder<T?> {
/// Adds a validation rule that checks if the value is null.
///
/// [message] is the error message returned if the validation fails. Defaults to "Must be null".
Expand All @@ -14,10 +14,11 @@ extension IsNullValidation<T, E> on LucidValidationBuilder<T?, E> {
///
/// Example:
/// ```dart
/// final builder = LucidValidationBuilder<String?>(key: 'optionalField');
/// builder.isNull();
/// ...
/// ruleFor((user) => user.name, key: 'name') // optional field
/// .isNull();
/// ```
LucidValidationBuilder<T?, E> isNull({String message = 'Must be null', String code = 'must_be_null'}) {
SimpleValidationBuilder<T?> isNull({String message = 'Must be null', String code = 'must_be_null'}) {
return must(
(value) => value == null,
message,
Expand Down
9 changes: 5 additions & 4 deletions lib/src/validations/less_then_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ part of 'validations.dart';
///
/// This extension adds a `lessThan` method that can be used to ensure that a number
/// is less than a specified value.
extension LessThanValidation on LucidValidationBuilder<num, dynamic> {
extension LessThanValidation on SimpleValidationBuilder<num> {
/// Adds a validation rule that checks if the [num] is less than [maxValue].
///
/// [maxValue] is the value that the number must be less than.
Expand All @@ -15,10 +15,11 @@ extension LessThanValidation on LucidValidationBuilder<num, dynamic> {
///
/// Example:
/// ```dart
/// final builder = LucidValidationBuilder<num>(key: 'discount');
/// builder.lessThan(100);
/// ...
/// ruleFor((user) => user.discount, key: 'discount')
/// .lessThan(100);
/// ```
LucidValidationBuilder<num, dynamic> lessThan(num maxValue, {String message = r'Must be less than $maxValue', String code = 'less_than'}) {
SimpleValidationBuilder<num> lessThan(num maxValue, {String message = r'Must be less than $maxValue', String code = 'less_than'}) {
return must(
(value) => value < maxValue,
message.replaceAll('$maxValue', maxValue.toString()),
Expand Down
9 changes: 5 additions & 4 deletions lib/src/validations/matches_pattern_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ part of 'validations.dart';
///
/// This extension adds a `matchesPattern` method that can be used to ensure that a string
/// matches a specific regex pattern.
extension MatchesPatternValidation on LucidValidationBuilder<String, dynamic> {
extension MatchesPatternValidation on SimpleValidationBuilder<String> {
/// Adds a validation rule that checks if the [String] matches the [pattern].
///
/// [pattern] is the regex pattern that the string must match.
Expand All @@ -15,10 +15,11 @@ extension MatchesPatternValidation on LucidValidationBuilder<String, dynamic> {
///
/// Example:
/// ```dart
/// final builder = LucidValidationBuilder<String>(key: 'phoneNumber');
/// builder.matchesPattern(r'^\d{3}-\d{3}-\d{4}$');
/// ...
/// ruleFor((user) => user.phoneNumber, key: 'phoneNumber')
/// .matchesPattern(r'^\d{3}-\d{3}-\d{4}$');
/// ```
LucidValidationBuilder<String, dynamic> matchesPattern(String pattern, {String message = 'Invalid format', String code = 'invalid_format'}) {
SimpleValidationBuilder<String> matchesPattern(String pattern, {String message = 'Invalid format', String code = 'invalid_format'}) {
return must(
(value) => RegExp(pattern).hasMatch(value),
message,
Expand Down
9 changes: 5 additions & 4 deletions lib/src/validations/max_length_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ part of 'validations.dart';
///
/// This extension adds a `maxLength` method that can be used to ensure that the length of a string
/// does not exceed a specified maximum number of characters.
extension MaxLengthValidation on LucidValidationBuilder<String, dynamic> {
extension MaxLengthValidation on SimpleValidationBuilder<String> {
/// Adds a validation rule that checks if the length of a [String] is less than or equal to [num].
///
/// [num] is the maximum allowed length for the string.
Expand All @@ -15,10 +15,11 @@ extension MaxLengthValidation on LucidValidationBuilder<String, dynamic> {
///
/// Example:
/// ```dart
/// final builder = LucidValidationBuilder<String>(key: 'username');
/// builder.maxLength(10);
/// ...
/// ruleFor((user) => user.username, key: 'username')
/// .maxLength(10);
/// ```
LucidValidationBuilder<String, dynamic> maxLength(int num, {String message = r'Must be at most $num characters long', String code = 'max_length'}) {
SimpleValidationBuilder<String> maxLength(int num, {String message = r'Must be at most $num characters long', String code = 'max_length'}) {
return must(
(value) => value.length <= num,
message.replaceAll(r'$num', num.toString()),
Expand Down
9 changes: 5 additions & 4 deletions lib/src/validations/max_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ part of 'validations.dart';
///
/// This extension adds a `max` method that can be used to ensure that a numerical value
/// does not exceed a specified maximum.
extension MaxValidation on LucidValidationBuilder<num, dynamic> {
extension MaxValidation on SimpleValidationBuilder<num> {
/// Adds a validation rule that checks if a [num] value is less than or equal to [num].
///
/// [num] is the maximum allowed value.
Expand All @@ -15,10 +15,11 @@ extension MaxValidation on LucidValidationBuilder<num, dynamic> {
///
/// Example:
/// ```dart
/// final builder = LucidValidationBuilder<num>(key: 'age');
/// builder.max(18);
/// ...
/// ruleFor((user) => user.age, key: 'age')
/// .maxLength(18);
/// ```
LucidValidationBuilder<num, dynamic> max(num num, {String message = r'Must be less than or equal to $num', String code = 'max_value'}) {
SimpleValidationBuilder<num> max(num num, {String message = r'Must be less than or equal to $num', String code = 'max_value'}) {
return must(
(value) => value <= num,
message.replaceAll(r'$num', num.toString()),
Expand Down
9 changes: 5 additions & 4 deletions lib/src/validations/min_length_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ part of 'validations.dart';
///
/// This extension adds a `minLength` method that can be used to ensure that the length of a string
/// meets a specified minimum number of characters.
extension MinLengthValidation on LucidValidationBuilder<String, dynamic> {
extension MinLengthValidation on SimpleValidationBuilder<String> {
/// Adds a validation rule that checks if the length of a [String] is greater than or equal to [num].
///
/// [num] is the minimum required length for the string.
Expand All @@ -15,10 +15,11 @@ extension MinLengthValidation on LucidValidationBuilder<String, dynamic> {
///
/// Example:
/// ```dart
/// final builder = LucidValidationBuilder<String>(key: 'password');
/// builder.minLength(8);
/// ...
/// ruleFor((user) => user.password, key: 'password')
/// .maxLength(8);
/// ```
LucidValidationBuilder<String, dynamic> minLength(int num, {String message = r'Must be at least $num characters long', String code = 'min_length'}) {
SimpleValidationBuilder<String> minLength(int num, {String message = r'Must be at least $num characters long', String code = 'min_length'}) {
return must(
(value) => value.length >= num,
message.replaceAll(r'$num', num.toString()),
Expand Down
9 changes: 5 additions & 4 deletions lib/src/validations/min_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ part of 'validations.dart';
///
/// This extension adds a `min` method that can be used to ensure that a numerical value
/// meets or exceeds a specified minimum.
extension MinValidation on LucidValidationBuilder<num, dynamic> {
extension MinValidation on SimpleValidationBuilder<num> {
/// Adds a validation rule that checks if a [num] value is greater than or equal to [num].
///
/// [num] is the minimum allowed value.
Expand All @@ -15,10 +15,11 @@ extension MinValidation on LucidValidationBuilder<num, dynamic> {
///
/// Example:
/// ```dart
/// final builder = LucidValidationBuilder<num>(key: 'age');
/// builder.min(18);
/// ...
/// ruleFor((user) => user.age, key: 'age')
/// .maxLength(18);
/// ```
LucidValidationBuilder<num, dynamic> min(num num, {String message = r'Must be greater than or equal to $num', String code = 'min_value'}) {
SimpleValidationBuilder<num> min(num num, {String message = r'Must be greater than or equal to $num', String code = 'min_value'}) {
return must(
(value) => value >= num,
message.replaceAll(r'$num', num.toString()),
Expand Down
9 changes: 5 additions & 4 deletions lib/src/validations/must_have_lowercase_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ part of 'validations.dart';
///
/// This extension adds a `mustHaveLowercase` method that can be used to ensure that a string
/// contains at least one lowercase letter.
extension MustHaveLowercaseValidation on LucidValidationBuilder<String, dynamic> {
extension MustHaveLowercaseValidation on SimpleValidationBuilder<String> {
/// Adds a validation rule that checks if the [String] contains at least one lowercase letter.
///
/// [message] is the error message returned if the validation fails. Defaults to "Must contain at least one lowercase letter".
Expand All @@ -14,10 +14,11 @@ extension MustHaveLowercaseValidation on LucidValidationBuilder<String, dynamic>
///
/// Example:
/// ```dart
/// final builder = LucidValidationBuilder<String>(key: 'password');
/// builder.mustHaveLowercase();
/// ...
/// ruleFor((user) => user.password, key: 'password')
/// .mustHaveLowercase();
/// ```
LucidValidationBuilder<String, dynamic> mustHaveLowercase({String message = 'Must contain at least one lowercase letter', String code = 'must_have_lowercase'}) {
SimpleValidationBuilder<String> mustHaveLowercase({String message = 'Must contain at least one lowercase letter', String code = 'must_have_lowercase'}) {
return must(
(value) => RegExp(r'[a-z]').hasMatch(value),
message,
Expand Down
9 changes: 5 additions & 4 deletions lib/src/validations/must_have_numbers_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ part of 'validations.dart';
///
/// This extension adds a `mustHaveNumbers` method that can be used to ensure that a string
/// contains at least one numeric digit.
extension MustHaveNumbersValidation on LucidValidationBuilder<String, dynamic> {
extension MustHaveNumbersValidation on SimpleValidationBuilder<String> {
/// Adds a validation rule that checks if the [String] contains at least one numeric digit.
///
/// [message] is the error message returned if the validation fails. Defaults to "Must contain at least one numeric digit".
Expand All @@ -14,10 +14,11 @@ extension MustHaveNumbersValidation on LucidValidationBuilder<String, dynamic> {
///
/// Example:
/// ```dart
/// final builder = LucidValidationBuilder<String>(key: 'password');
/// builder.mustHaveNumbers();
/// ...
/// ruleFor((user) => user.password, key: 'password')
/// .mustHaveNumbers();
/// ```
LucidValidationBuilder<String, dynamic> mustHaveNumbers({String message = 'Must contain at least one numeric digit', String code = 'must_have_numbers'}) {
SimpleValidationBuilder<String> mustHaveNumbers({String message = 'Must contain at least one numeric digit', String code = 'must_have_numbers'}) {
return must(
(value) => RegExp(r'[0-9]').hasMatch(value),
message,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ part of 'validations.dart';
///
/// This extension adds a `mustHaveSpecialCharacter` method that can be used to ensure that a string
/// contains at least one special character.
extension MustHaveSpecialCharacterValidation on LucidValidationBuilder<String, dynamic> {
extension MustHaveSpecialCharacterValidation on SimpleValidationBuilder<String> {
/// Adds a validation rule that checks if the [String] contains at least one special character.
///
/// [message] is the error message returned if the validation fails. Defaults to "Must contain at least one special character".
Expand All @@ -14,10 +14,11 @@ extension MustHaveSpecialCharacterValidation on LucidValidationBuilder<String, d
///
/// Example:
/// ```dart
/// final builder = LucidValidationBuilder<String>(key: 'password');
/// builder.mustHaveSpecialCharacter();
/// ...
/// ruleFor((user) => user.password, key: 'password')
/// .mustHaveSpecialCharacter();
/// ```
LucidValidationBuilder<String, dynamic> mustHaveSpecialCharacter({String message = 'Must contain at least one special character', String code = 'must_have_special_character'}) {
SimpleValidationBuilder<String> mustHaveSpecialCharacter({String message = 'Must contain at least one special character', String code = 'must_have_special_character'}) {
return must(
(value) => RegExp(r'[!@#\$%\^&\*(),.?":{}|<>]').hasMatch(value),
message,
Expand Down
9 changes: 5 additions & 4 deletions lib/src/validations/must_have_uppercase_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ part of 'validations.dart';
///
/// This extension adds a `mustHaveUppercase` method that can be used to ensure that a string
/// contains at least one uppercase letter.
extension MustHaveUppercaseValidation on LucidValidationBuilder<String, dynamic> {
extension MustHaveUppercaseValidation on SimpleValidationBuilder<String> {
/// Adds a validation rule that checks if the [String] contains at least one uppercase letter.
///
/// [message] is the error message returned if the validation fails. Defaults to "Must contain at least one uppercase letter".
Expand All @@ -14,10 +14,11 @@ extension MustHaveUppercaseValidation on LucidValidationBuilder<String, dynamic>
///
/// Example:
/// ```dart
/// final builder = LucidValidationBuilder<String>(key: 'password');
/// builder.mustHaveUppercase();
/// ...
/// ruleFor((user) => user.password, key: 'password')
/// .mustHaveUppercase();
/// ```
LucidValidationBuilder<String, dynamic> mustHaveUppercase({String message = 'Must contain at least one uppercase letter', String code = 'must_have_uppercase'}) {
SimpleValidationBuilder<String> mustHaveUppercase({String message = 'Must contain at least one uppercase letter', String code = 'must_have_uppercase'}) {
return must(
(value) => RegExp(r'[A-Z]').hasMatch(value),
message,
Expand Down
9 changes: 5 additions & 4 deletions lib/src/validations/not_empty_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ part of 'validations.dart';
///
/// This extension adds a `notEmpty` method that can be used to ensure that a string
/// is not empty.
extension NotEmptyValidation on LucidValidationBuilder<String, dynamic> {
extension NotEmptyValidation on SimpleValidationBuilder<String> {
/// Adds a validation rule that checks if the [String] is not empty.
///
/// [message] is the error message returned if the validation fails. Defaults to "Cannot be empty".
Expand All @@ -14,10 +14,11 @@ extension NotEmptyValidation on LucidValidationBuilder<String, dynamic> {
///
/// Example:
/// ```dart
/// final builder = LucidValidationBuilder<String>(key: 'username');
/// builder.notEmpty();
/// ...
/// ruleFor((user) => user.username, key: 'username')
/// .notEmpty();
/// ```
LucidValidationBuilder<String, dynamic> notEmpty({String message = 'Cannot be empty', String code = 'not_empty'}) {
SimpleValidationBuilder<String> notEmpty({String message = 'Cannot be empty', String code = 'not_empty'}) {
return must(
(value) => value.isNotEmpty,
message,
Expand Down
5 changes: 3 additions & 2 deletions lib/src/validations/not_equal_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ extension NotEqualValidation<T, E> on LucidValidationBuilder<T, E> {
///
/// Example:
/// ```dart
/// final builder = LucidValidationBuilder<String>(key: 'newUsername');
/// builder.notEqualTo('oldUsername');
/// ...
/// ruleFor((user) => user.newUsername, key: 'newUsername')
/// .notEqualTo((user) => user.oldUsername);
/// ```
LucidValidationBuilder<T, dynamic> notEqualTo(T Function(E entity) predicate, {String message = r'Must not be equal', String code = 'not_equal_to_error'}) {
return mustWith(
Expand Down
Loading

0 comments on commit 40aa858

Please sign in to comment.