From 40aa858b83b7c03b7cca732aa92050204880ea14 Mon Sep 17 00:00:00 2001 From: wellgenio Date: Sun, 25 Aug 2024 12:51:48 -0300 Subject: [PATCH] fix validations --- example/lib/domain/dtos/login_param_dto.dart | 2 +- lib/src/validations/equal_validation.dart | 6 ++++-- lib/src/validations/greater_than_validation.dart | 9 +++++---- lib/src/validations/is_empty_validation.dart | 9 +++++---- lib/src/validations/is_not_null_validation.dart | 9 +++++---- lib/src/validations/is_null_validation.dart | 9 +++++---- lib/src/validations/less_then_validation.dart | 9 +++++---- lib/src/validations/matches_pattern_validation.dart | 9 +++++---- lib/src/validations/max_length_validation.dart | 9 +++++---- lib/src/validations/max_validation.dart | 9 +++++---- lib/src/validations/min_length_validation.dart | 9 +++++---- lib/src/validations/min_validation.dart | 9 +++++---- .../validations/must_have_lowercase_validation.dart | 9 +++++---- lib/src/validations/must_have_numbers_validation.dart | 9 +++++---- .../must_have_special_character_validation.dart | 9 +++++---- .../validations/must_have_uppercase_validation.dart | 9 +++++---- lib/src/validations/not_empty_validation.dart | 9 +++++---- lib/src/validations/not_equal_validation.dart | 5 +++-- lib/src/validations/range_validation.dart | 9 +++++---- lib/src/validations/valid_cep_validation.dart | 11 +++++++++-- lib/src/validations/valid_cnpj_validation.dart | 11 +++++++++-- lib/src/validations/valid_cpf_validation.dart | 9 +++++---- lib/src/validations/valid_creditcard_validation.dart | 11 +++++++++-- lib/src/validations/valid_email_validation.dart | 9 +++++---- 24 files changed, 125 insertions(+), 83 deletions(-) diff --git a/example/lib/domain/dtos/login_param_dto.dart b/example/lib/domain/dtos/login_param_dto.dart index a45ea7b..09f769c 100644 --- a/example/lib/domain/dtos/login_param_dto.dart +++ b/example/lib/domain/dtos/login_param_dto.dart @@ -11,5 +11,5 @@ class LoginParamDto { setEmail(String value) => email = value; - setPassword(String value) => email = value; + setPassword(String value) => password = value; } diff --git a/lib/src/validations/equal_validation.dart b/lib/src/validations/equal_validation.dart index 8982ec6..5c3f82b 100644 --- a/lib/src/validations/equal_validation.dart +++ b/lib/src/validations/equal_validation.dart @@ -15,8 +15,10 @@ extension EqualValidation on LucidValidationBuilder { /// /// Example: /// ```dart - /// final builder = LucidValidationBuilder(key: 'confirmPassword'); - /// builder.equalTo('password123'); + /// ... + /// ruleFor((user) => user.confirmPassword, key: 'confirmPassword') + /// .equalTo((user) => user.password); + /// /// ``` LucidValidationBuilder equalTo(T Function(E entity) predicate, {String message = r'Must be equal', String code = 'equal_error'}) { return mustWith( diff --git a/lib/src/validations/greater_than_validation.dart b/lib/src/validations/greater_than_validation.dart index edd97c7..922084f 100644 --- a/lib/src/validations/greater_than_validation.dart +++ b/lib/src/validations/greater_than_validation.dart @@ -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 { +extension GreaterThanValidation on SimpleValidationBuilder { /// Adds a validation rule that checks if the [num] is greater than [minValue]. /// /// [minValue] is the value that the number must be greater than. @@ -15,10 +15,11 @@ extension GreaterThanValidation on LucidValidationBuilder { /// /// Example: /// ```dart - /// final builder = LucidValidationBuilder(key: 'age'); - /// builder.greaterThan(18); + /// ... + /// ruleFor((user) => user.age, key: 'age') + /// .greaterThan(18); /// ``` - LucidValidationBuilder greaterThan(num minValue, {String message = r'Must be greater than $minValue', String code = 'greater_than'}) { + SimpleValidationBuilder 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()), diff --git a/lib/src/validations/is_empty_validation.dart b/lib/src/validations/is_empty_validation.dart index 65e7691..92e4229 100644 --- a/lib/src/validations/is_empty_validation.dart +++ b/lib/src/validations/is_empty_validation.dart @@ -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 { +extension IsEmptyValidation on SimpleValidationBuilder { /// 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". @@ -14,10 +14,11 @@ extension IsEmptyValidation on LucidValidationBuilder { /// /// Example: /// ```dart - /// final builder = LucidValidationBuilder(key: 'field'); - /// builder.isEmpty(); + /// ... + /// ruleFor((user) => user.name, key: 'name') + /// .isEmpty(); /// ``` - LucidValidationBuilder isEmpty({String message = 'Must be empty', String code = 'must_be_empty'}) { + SimpleValidationBuilder isEmpty({String message = 'Must be empty', String code = 'must_be_empty'}) { return must( (value) => value.isEmpty, message, diff --git a/lib/src/validations/is_not_null_validation.dart b/lib/src/validations/is_not_null_validation.dart index f5510c4..c9df9a1 100644 --- a/lib/src/validations/is_not_null_validation.dart +++ b/lib/src/validations/is_not_null_validation.dart @@ -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 on LucidValidationBuilder { +extension IsNotNullValidation on SimpleValidationBuilder { /// 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". @@ -14,10 +14,11 @@ extension IsNotNullValidation on LucidValidationBuilder { /// /// Example: /// ```dart - /// final builder = LucidValidationBuilder(key: 'requiredField'); - /// builder.isNotNull(); + /// ... + /// ruleFor((user) => user.name, key: 'name') // required field + /// .isNotNull(); /// ``` - LucidValidationBuilder isNotNull({String message = 'Cannot be null', String code = 'cannot_be_null'}) { + SimpleValidationBuilder isNotNull({String message = 'Cannot be null', String code = 'cannot_be_null'}) { return must( (value) => value != null, message, diff --git a/lib/src/validations/is_null_validation.dart b/lib/src/validations/is_null_validation.dart index 95a8f93..c519509 100644 --- a/lib/src/validations/is_null_validation.dart +++ b/lib/src/validations/is_null_validation.dart @@ -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 on LucidValidationBuilder { +extension IsNullValidation on SimpleValidationBuilder { /// 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". @@ -14,10 +14,11 @@ extension IsNullValidation on LucidValidationBuilder { /// /// Example: /// ```dart - /// final builder = LucidValidationBuilder(key: 'optionalField'); - /// builder.isNull(); + /// ... + /// ruleFor((user) => user.name, key: 'name') // optional field + /// .isNull(); /// ``` - LucidValidationBuilder isNull({String message = 'Must be null', String code = 'must_be_null'}) { + SimpleValidationBuilder isNull({String message = 'Must be null', String code = 'must_be_null'}) { return must( (value) => value == null, message, diff --git a/lib/src/validations/less_then_validation.dart b/lib/src/validations/less_then_validation.dart index 52d5f4d..bc3199d 100644 --- a/lib/src/validations/less_then_validation.dart +++ b/lib/src/validations/less_then_validation.dart @@ -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 { +extension LessThanValidation on SimpleValidationBuilder { /// Adds a validation rule that checks if the [num] is less than [maxValue]. /// /// [maxValue] is the value that the number must be less than. @@ -15,10 +15,11 @@ extension LessThanValidation on LucidValidationBuilder { /// /// Example: /// ```dart - /// final builder = LucidValidationBuilder(key: 'discount'); - /// builder.lessThan(100); + /// ... + /// ruleFor((user) => user.discount, key: 'discount') + /// .lessThan(100); /// ``` - LucidValidationBuilder lessThan(num maxValue, {String message = r'Must be less than $maxValue', String code = 'less_than'}) { + SimpleValidationBuilder 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()), diff --git a/lib/src/validations/matches_pattern_validation.dart b/lib/src/validations/matches_pattern_validation.dart index c15ff53..a728713 100644 --- a/lib/src/validations/matches_pattern_validation.dart +++ b/lib/src/validations/matches_pattern_validation.dart @@ -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 { +extension MatchesPatternValidation on SimpleValidationBuilder { /// Adds a validation rule that checks if the [String] matches the [pattern]. /// /// [pattern] is the regex pattern that the string must match. @@ -15,10 +15,11 @@ extension MatchesPatternValidation on LucidValidationBuilder { /// /// Example: /// ```dart - /// final builder = LucidValidationBuilder(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 matchesPattern(String pattern, {String message = 'Invalid format', String code = 'invalid_format'}) { + SimpleValidationBuilder matchesPattern(String pattern, {String message = 'Invalid format', String code = 'invalid_format'}) { return must( (value) => RegExp(pattern).hasMatch(value), message, diff --git a/lib/src/validations/max_length_validation.dart b/lib/src/validations/max_length_validation.dart index bdd151a..f13e6b4 100644 --- a/lib/src/validations/max_length_validation.dart +++ b/lib/src/validations/max_length_validation.dart @@ -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 { +extension MaxLengthValidation on SimpleValidationBuilder { /// 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. @@ -15,10 +15,11 @@ extension MaxLengthValidation on LucidValidationBuilder { /// /// Example: /// ```dart - /// final builder = LucidValidationBuilder(key: 'username'); - /// builder.maxLength(10); + /// ... + /// ruleFor((user) => user.username, key: 'username') + /// .maxLength(10); /// ``` - LucidValidationBuilder maxLength(int num, {String message = r'Must be at most $num characters long', String code = 'max_length'}) { + SimpleValidationBuilder 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()), diff --git a/lib/src/validations/max_validation.dart b/lib/src/validations/max_validation.dart index c8bf40d..4630c8e 100644 --- a/lib/src/validations/max_validation.dart +++ b/lib/src/validations/max_validation.dart @@ -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 { +extension MaxValidation on SimpleValidationBuilder { /// Adds a validation rule that checks if a [num] value is less than or equal to [num]. /// /// [num] is the maximum allowed value. @@ -15,10 +15,11 @@ extension MaxValidation on LucidValidationBuilder { /// /// Example: /// ```dart - /// final builder = LucidValidationBuilder(key: 'age'); - /// builder.max(18); + /// ... + /// ruleFor((user) => user.age, key: 'age') + /// .maxLength(18); /// ``` - LucidValidationBuilder max(num num, {String message = r'Must be less than or equal to $num', String code = 'max_value'}) { + SimpleValidationBuilder 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()), diff --git a/lib/src/validations/min_length_validation.dart b/lib/src/validations/min_length_validation.dart index 7862981..c96fe8e 100644 --- a/lib/src/validations/min_length_validation.dart +++ b/lib/src/validations/min_length_validation.dart @@ -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 { +extension MinLengthValidation on SimpleValidationBuilder { /// 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. @@ -15,10 +15,11 @@ extension MinLengthValidation on LucidValidationBuilder { /// /// Example: /// ```dart - /// final builder = LucidValidationBuilder(key: 'password'); - /// builder.minLength(8); + /// ... + /// ruleFor((user) => user.password, key: 'password') + /// .maxLength(8); /// ``` - LucidValidationBuilder minLength(int num, {String message = r'Must be at least $num characters long', String code = 'min_length'}) { + SimpleValidationBuilder 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()), diff --git a/lib/src/validations/min_validation.dart b/lib/src/validations/min_validation.dart index fda2fd2..e00f182 100644 --- a/lib/src/validations/min_validation.dart +++ b/lib/src/validations/min_validation.dart @@ -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 { +extension MinValidation on SimpleValidationBuilder { /// Adds a validation rule that checks if a [num] value is greater than or equal to [num]. /// /// [num] is the minimum allowed value. @@ -15,10 +15,11 @@ extension MinValidation on LucidValidationBuilder { /// /// Example: /// ```dart - /// final builder = LucidValidationBuilder(key: 'age'); - /// builder.min(18); + /// ... + /// ruleFor((user) => user.age, key: 'age') + /// .maxLength(18); /// ``` - LucidValidationBuilder min(num num, {String message = r'Must be greater than or equal to $num', String code = 'min_value'}) { + SimpleValidationBuilder 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()), diff --git a/lib/src/validations/must_have_lowercase_validation.dart b/lib/src/validations/must_have_lowercase_validation.dart index b0de65e..22ac450 100644 --- a/lib/src/validations/must_have_lowercase_validation.dart +++ b/lib/src/validations/must_have_lowercase_validation.dart @@ -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 { +extension MustHaveLowercaseValidation on SimpleValidationBuilder { /// 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". @@ -14,10 +14,11 @@ extension MustHaveLowercaseValidation on LucidValidationBuilder /// /// Example: /// ```dart - /// final builder = LucidValidationBuilder(key: 'password'); - /// builder.mustHaveLowercase(); + /// ... + /// ruleFor((user) => user.password, key: 'password') + /// .mustHaveLowercase(); /// ``` - LucidValidationBuilder mustHaveLowercase({String message = 'Must contain at least one lowercase letter', String code = 'must_have_lowercase'}) { + SimpleValidationBuilder 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, diff --git a/lib/src/validations/must_have_numbers_validation.dart b/lib/src/validations/must_have_numbers_validation.dart index bc2172e..7aad95b 100644 --- a/lib/src/validations/must_have_numbers_validation.dart +++ b/lib/src/validations/must_have_numbers_validation.dart @@ -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 { +extension MustHaveNumbersValidation on SimpleValidationBuilder { /// 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". @@ -14,10 +14,11 @@ extension MustHaveNumbersValidation on LucidValidationBuilder { /// /// Example: /// ```dart - /// final builder = LucidValidationBuilder(key: 'password'); - /// builder.mustHaveNumbers(); + /// ... + /// ruleFor((user) => user.password, key: 'password') + /// .mustHaveNumbers(); /// ``` - LucidValidationBuilder mustHaveNumbers({String message = 'Must contain at least one numeric digit', String code = 'must_have_numbers'}) { + SimpleValidationBuilder 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, diff --git a/lib/src/validations/must_have_special_character_validation.dart b/lib/src/validations/must_have_special_character_validation.dart index 916f168..b43df30 100644 --- a/lib/src/validations/must_have_special_character_validation.dart +++ b/lib/src/validations/must_have_special_character_validation.dart @@ -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 { +extension MustHaveSpecialCharacterValidation on SimpleValidationBuilder { /// 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". @@ -14,10 +14,11 @@ extension MustHaveSpecialCharacterValidation on LucidValidationBuilder(key: 'password'); - /// builder.mustHaveSpecialCharacter(); + /// ... + /// ruleFor((user) => user.password, key: 'password') + /// .mustHaveSpecialCharacter(); /// ``` - LucidValidationBuilder mustHaveSpecialCharacter({String message = 'Must contain at least one special character', String code = 'must_have_special_character'}) { + SimpleValidationBuilder mustHaveSpecialCharacter({String message = 'Must contain at least one special character', String code = 'must_have_special_character'}) { return must( (value) => RegExp(r'[!@#\$%\^&\*(),.?":{}|<>]').hasMatch(value), message, diff --git a/lib/src/validations/must_have_uppercase_validation.dart b/lib/src/validations/must_have_uppercase_validation.dart index 5b5b47f..b2bc438 100644 --- a/lib/src/validations/must_have_uppercase_validation.dart +++ b/lib/src/validations/must_have_uppercase_validation.dart @@ -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 { +extension MustHaveUppercaseValidation on SimpleValidationBuilder { /// 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". @@ -14,10 +14,11 @@ extension MustHaveUppercaseValidation on LucidValidationBuilder /// /// Example: /// ```dart - /// final builder = LucidValidationBuilder(key: 'password'); - /// builder.mustHaveUppercase(); + /// ... + /// ruleFor((user) => user.password, key: 'password') + /// .mustHaveUppercase(); /// ``` - LucidValidationBuilder mustHaveUppercase({String message = 'Must contain at least one uppercase letter', String code = 'must_have_uppercase'}) { + SimpleValidationBuilder 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, diff --git a/lib/src/validations/not_empty_validation.dart b/lib/src/validations/not_empty_validation.dart index fb2e5f2..5936e8b 100644 --- a/lib/src/validations/not_empty_validation.dart +++ b/lib/src/validations/not_empty_validation.dart @@ -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 { +extension NotEmptyValidation on SimpleValidationBuilder { /// 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". @@ -14,10 +14,11 @@ extension NotEmptyValidation on LucidValidationBuilder { /// /// Example: /// ```dart - /// final builder = LucidValidationBuilder(key: 'username'); - /// builder.notEmpty(); + /// ... + /// ruleFor((user) => user.username, key: 'username') + /// .notEmpty(); /// ``` - LucidValidationBuilder notEmpty({String message = 'Cannot be empty', String code = 'not_empty'}) { + SimpleValidationBuilder notEmpty({String message = 'Cannot be empty', String code = 'not_empty'}) { return must( (value) => value.isNotEmpty, message, diff --git a/lib/src/validations/not_equal_validation.dart b/lib/src/validations/not_equal_validation.dart index 1da3c61..c7050e0 100644 --- a/lib/src/validations/not_equal_validation.dart +++ b/lib/src/validations/not_equal_validation.dart @@ -15,8 +15,9 @@ extension NotEqualValidation on LucidValidationBuilder { /// /// Example: /// ```dart - /// final builder = LucidValidationBuilder(key: 'newUsername'); - /// builder.notEqualTo('oldUsername'); + /// ... + /// ruleFor((user) => user.newUsername, key: 'newUsername') + /// .notEqualTo((user) => user.oldUsername); /// ``` LucidValidationBuilder notEqualTo(T Function(E entity) predicate, {String message = r'Must not be equal', String code = 'not_equal_to_error'}) { return mustWith( diff --git a/lib/src/validations/range_validation.dart b/lib/src/validations/range_validation.dart index c2f7e12..ef3d966 100644 --- a/lib/src/validations/range_validation.dart +++ b/lib/src/validations/range_validation.dart @@ -4,7 +4,7 @@ part of 'validations.dart'; /// /// This extension adds a `range` method that can be used to ensure that a number /// is within a specified range. -extension RangeValidation on LucidValidationBuilder { +extension RangeValidation on SimpleValidationBuilder { /// Adds a validation rule that checks if the [num] is within the range of [min] and [max]. /// /// [min] and [max] define the acceptable range for the number. @@ -15,10 +15,11 @@ extension RangeValidation on LucidValidationBuilder { /// /// Example: /// ```dart - /// final builder = LucidValidationBuilder(key: 'age'); - /// builder.range(18, 65); + /// ... + /// ruleFor((user) => user.age, key: 'age') + /// .range(18, 65); /// ``` - LucidValidationBuilder range(num min, num max, {String message = r'Must be between $min and $max', String code = 'range_error'}) { + SimpleValidationBuilder range(num min, num max, {String message = r'Must be between $min and $max', String code = 'range_error'}) { return must( (value) => value >= min && value <= max, message.replaceAll(r'$min', min.toString()).replaceAll('$max', max.toString()), diff --git a/lib/src/validations/valid_cep_validation.dart b/lib/src/validations/valid_cep_validation.dart index 8478911..d9753b7 100644 --- a/lib/src/validations/valid_cep_validation.dart +++ b/lib/src/validations/valid_cep_validation.dart @@ -1,6 +1,6 @@ part of 'validations.dart'; -extension ValidCEPValidation on LucidValidationBuilder { +extension ValidCEPValidation on SimpleValidationBuilder { /// 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 @@ -10,7 +10,14 @@ extension ValidCEPValidation on LucidValidationBuilder { /// [code] is an optional error code for translation purposes. /// /// Returns the [LucidValidationBuilder] to allow for method chaining. - LucidValidationBuilder validCEP({String message = 'Invalid CEP', String code = 'invalid_cep'}) { + /// + /// Example: + /// ```dart + /// ... + /// ruleFor((user) => user.cep, key: 'cep') + /// .validCEP(); + /// ``` + SimpleValidationBuilder validCEP({String message = 'Invalid CEP', String code = 'invalid_cep'}) { return must( (value) => RegExp(r'^\d{5}-?\d{3}$').hasMatch(value), message, diff --git a/lib/src/validations/valid_cnpj_validation.dart b/lib/src/validations/valid_cnpj_validation.dart index 40910d5..dc4b293 100644 --- a/lib/src/validations/valid_cnpj_validation.dart +++ b/lib/src/validations/valid_cnpj_validation.dart @@ -1,6 +1,6 @@ part of 'validations.dart'; -extension ValidCnpjValidation on LucidValidationBuilder { +extension ValidCnpjValidation on SimpleValidationBuilder { /// 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 @@ -11,7 +11,14 @@ extension ValidCnpjValidation on LucidValidationBuilder { /// [code] is an optional error code for translation purposes. /// /// Returns the [LucidValidationBuilder] to allow for method chaining. - LucidValidationBuilder validCNPJ({String message = 'Invalid CNPJ', String code = 'invalid_cnpj'}) { + /// + /// Example: + /// ```dart + /// ... + /// ruleFor((user) => user.cnpj, key: 'cnpj') + /// .validCNPJ(); + /// ``` + SimpleValidationBuilder validCNPJ({String message = 'Invalid CNPJ', String code = 'invalid_cnpj'}) { return must( (value) => _validateCNPJ(value), message, diff --git a/lib/src/validations/valid_cpf_validation.dart b/lib/src/validations/valid_cpf_validation.dart index 769fa1f..6916be3 100644 --- a/lib/src/validations/valid_cpf_validation.dart +++ b/lib/src/validations/valid_cpf_validation.dart @@ -4,7 +4,7 @@ part of 'validations.dart'; /// /// This extension adds a `validCPF` method that can be used to ensure that a string /// is a valid CPF number. -extension ValidCPFValidation on LucidValidationBuilder { +extension ValidCPFValidation on SimpleValidationBuilder { /// Adds a validation rule that checks if the [String] is a valid CPF number. /// /// [message] is the error message returned if the validation fails. Defaults to "Invalid CPF". @@ -14,10 +14,11 @@ extension ValidCPFValidation on LucidValidationBuilder { /// /// Example: /// ```dart - /// final builder = LucidValidationBuilder(key: 'cpf'); - /// builder.validCPF(); + /// ... + /// ruleFor((user) => user.cpf, key: 'cpf') + /// .validCPF(); /// ``` - LucidValidationBuilder validCPF({String message = 'Invalid CPF', String code = 'invalid_cpf'}) { + SimpleValidationBuilder validCPF({String message = 'Invalid CPF', String code = 'invalid_cpf'}) { return must( (value) => _validateCPF(value), message, diff --git a/lib/src/validations/valid_creditcard_validation.dart b/lib/src/validations/valid_creditcard_validation.dart index dfc6bdb..094ce9f 100644 --- a/lib/src/validations/valid_creditcard_validation.dart +++ b/lib/src/validations/valid_creditcard_validation.dart @@ -1,6 +1,6 @@ part of 'validations.dart'; -extension ValidCreditCardValidation on LucidValidationBuilder { +extension ValidCreditCardValidation on SimpleValidationBuilder { /// 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. @@ -10,7 +10,14 @@ extension ValidCreditCardValidation on LucidValidationBuilder { /// [code] is an optional error code for translation purposes. /// /// Returns the [LucidValidationBuilder] to allow for method chaining. - LucidValidationBuilder validCreditCard({String message = 'Invalid credit card number', String code = 'invalid_credit_card'}) { + + /// Example: + /// ```dart + /// ... + /// ruleFor((user) => user.creditCard, key: 'creditCard') + /// .validCreditCard(); + /// ``` + SimpleValidationBuilder validCreditCard({String message = 'Invalid credit card number', String code = 'invalid_credit_card'}) { return must( (value) => _validateCreditCard(value), message, diff --git a/lib/src/validations/valid_email_validation.dart b/lib/src/validations/valid_email_validation.dart index 202d2c1..2e80063 100644 --- a/lib/src/validations/valid_email_validation.dart +++ b/lib/src/validations/valid_email_validation.dart @@ -4,7 +4,7 @@ part of 'validations.dart'; /// /// This extension adds a `validEmail` method that can be used to ensure that a string /// is a valid email address. -extension ValidEmailValidation on LucidValidationBuilder { +extension ValidEmailValidation on SimpleValidationBuilder { /// Adds a validation rule that checks if the [String] is a valid email address. /// /// [message] is the error message returned if the validation fails. Defaults to "Invalid email address". @@ -14,10 +14,11 @@ extension ValidEmailValidation on LucidValidationBuilder { /// /// Example: /// ```dart - /// final builder = LucidValidationBuilder(key: 'email'); - /// builder.validEmail(); + /// ... + /// ruleFor((user) => user.email, key: 'email') + /// .validEmail(); /// ``` - LucidValidationBuilder validEmail({String message = 'Invalid email address', String code = 'invalid_email'}) { + SimpleValidationBuilder validEmail({String message = 'Invalid email address', String code = 'invalid_email'}) { return must( (value) => RegExp(r'^[\w\.-]+@[a-zA-Z\d\.-]+\.[a-zA-Z]{2,}$').hasMatch(value), message,