Skip to content

Commit ddd9cbd

Browse files
update string session
1 parent 960707b commit ddd9cbd

File tree

3 files changed

+71
-102
lines changed

3 files changed

+71
-102
lines changed

README-updated.md

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,12 @@ Also it includes the `l10n` / `i18n` of error text messages to multiple language
6060

6161
## Validators
6262

63-
This package comes with several most common `FormFieldValidator`s such as required, numeric, mail,
63+
This package comes with several most common `FormFieldValidator`s such as required, numeric, email,
6464
URL, min, max, minLength, maxLength, minWordsCount, maxWordsCount, IP, credit card, etc., with default `errorText` messages.
6565

6666

6767
### Bool validators
6868

69-
- `FormBuilderValidators.hasLowercaseChars()` - requires the field's to contain a specified number of lowercase characters.
70-
- `FormBuilderValidators.hasNumericChars()` - requires the field's to contain a specified number of numeric characters.
71-
- `FormBuilderValidators.hasSpecialChars()` - requires the field's to contain a specified number of special characters.
72-
- `FormBuilderValidators.hasUppercaseChars()` - requires the field's to contain a specified number of uppercase characters.
7369
- `FormBuilderValidators.isFalse()` - requires the field's to be false.
7470
- `FormBuilderValidators.isTrue()` - requires the field's to be true.
7571

@@ -163,17 +159,22 @@ URL, min, max, minLength, maxLength, minWordsCount, maxWordsCount, IP, credit ca
163159

164160
### String validators
165161

166-
- `FormBuilderValidators.alphabetical()` - requires the field's to contain only alphabetical characters.
167-
- `FormBuilderValidators.contains()` - requires the substring to be in the field's value.
168-
- `FormBuilderValidators.endsWith()` - requires the substring to be the end of the field's value.
169-
- `FormBuilderValidators.lowercase()` - requires the field's value to be lowercase.
170-
- `FormBuilderValidators.matchNot()` - requires the field's value to not match the provided regex pattern.
171-
- `FormBuilderValidators.match()` - requires the field's value to match the provided regex pattern.
172-
- `FormBuilderValidators.maxWordsCount()` - requires the word count of the field's value to be less than or equal to the provided maximum count.
173-
- `FormBuilderValidators.minWordsCount()` - requires the word count of the field's value to be greater than or equal to the provided minimum count.
174-
- `FormBuilderValidators.singleLine()` - requires the field's string to be a single line of text.
175-
- `FormBuilderValidators.startsWith()` - requires the substring to be the start of the field's value.
176-
- `FormBuilderValidators.uppercase()` - requires the field's value to be uppercase.
162+
- `Validators.contains(substring)` - Checks if the field contains the `substring`.
163+
- TODO `FormBuilderValidators.endsWith()` - requires the substring to be the end of the field's value.
164+
- TODO `FormBuilderValidators.startsWith()` - requires the substring to be the start of the field's value.
165+
- TODO `FormBuilderValidators.lowercase()` - requires the field's value to be lowercase.
166+
- TODO `FormBuilderValidators.uppercase()` - requires the field's value to be uppercase.
167+
- `Validators.hasMinUppercaseChars(min:min)` - Checks if the field has a minimum number of uppercase chars.
168+
- `Validators.hasMinLowercaseChars(min:min)` - Checks if the field has a minimum number of lowercase chars.
169+
- `Validators.hasMinNumericChars(min:min)` - Checks if the field has a minimum number of numeric chars.
170+
- `Validators.hasMinSpecialChars(min:min)` - Checks if the field has a minimum number of special chars.
171+
- `Validators.match(regExp)` - Checks if the field matches with the regular expression `regExp`.
172+
- TODO `FormBuilderValidators.matchNot()` - requires the field's value to not match the provided regex pattern.
173+
- `Validators.uuid()` - Checks if the field is a valid Universally Unique Identifier (UUID).
174+
- TODO `FormBuilderValidators.alphabetical()` - requires the field's to contain only alphabetical characters.
175+
- TODO `FormBuilderValidators.maxWordsCount()` - requires the word count of the field's value to be less than or equal to the provided maximum count.
176+
- TODO `FormBuilderValidators.minWordsCount()` - requires the word count of the field's value to be greater than or equal to the provided minimum count.
177+
- TODO `FormBuilderValidators.singleLine()` - requires the field's string to be a single line of text.
177178

178179
### Use-case validators
179180

@@ -484,35 +485,3 @@ Take a look at [our fantastic ecosystem](https://github.com/flutter-form-builder
484485
[All contributors](https://github.com/flutter-form-builder-ecosystem/form_builder_validators/graphs/contributors)
485486

486487

487-
# API changes draft
488-
During the process of exploration of new possibilities for the new API, I realized that there are
489-
basically three layers of validators: required layer, type layer and the specialized layer. Instead of
490-
repeating the computations for required and type layer for each validator composition, it is possible
491-
to decouple them, avoiding this redundancy and taking benefits from the Dart compiler.
492-
493-
During the exploration, I implemented some elementary validators that would make it possible, by
494-
composition, to create more sophisticated validators. The recipe is simple, start with a (not)required
495-
validator, add a type validator, and then chain as many specialized validators as you want.
496-
497-
```dart
498-
// In this example, we build a validator composing a required, with a numeric and then a max.
499-
// The logic result is: required && numeric && max(70)
500-
501-
final validator = ValidatorBuilder.required(and: <Validator<Object, num>>[
502-
ValidatorBuilder.numeric(
503-
errorText: 'La edad debe ser numérica.',
504-
and: <BaseElementaryValidator<num, dynamic>>[
505-
ValidatorBuilder.max(70),
506-
])
507-
]).validate;
508-
```
509-
510-
I needed to change a little bit the approach. Instead of composing directly the validators as
511-
FormFieldValidator's, one level of indirection was necessary, using a ValidatorBuilder instead.
512-
Thus, we first build the validator and then create the validation method calling validate.
513-
514-
I implemented some examples that are related to some examples from example/main.dart. The new
515-
API examples are implemented in example/api_refactoring_main.dart. I recorded a video showing the
516-
execution of the examples and explaining the new api ideas.
517-
518-
Please, give me the necessary feedback for me to continue the work.

lib/src/form_builder_validators.dart

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,56 +2659,6 @@ final class Validators {
26592659

26602660
// String validators
26612661

2662-
/// {@template validator_uuid}
2663-
/// A validator function that checks if a given string matches the UUID format.
2664-
///
2665-
/// Creates a validator that ensures the input string conforms to the standard
2666-
/// UUID (Universally Unique Identifier) format, consisting of 32 hexadecimal
2667-
/// digits displayed in 5 groups separated by hyphens (8-4-4-4-12).
2668-
///
2669-
/// ## Parameters
2670-
/// - `regex` (`RegExp?`): Optional custom regular expression pattern to override
2671-
/// the default UUID validation pattern. Useful for supporting different UUID
2672-
/// formats or adding additional constraints.
2673-
/// - `uuidMsg` (`String Function(String input)?`): Optional callback function that
2674-
/// generates a custom error message based on the invalid input. If not provided,
2675-
/// defaults to the standard form builder localization error text.
2676-
///
2677-
/// ## Returns
2678-
/// Returns a `Validator<String>` function that accepts a string input and returns:
2679-
/// - `null` if the input is valid
2680-
/// - An error message string if the input is invalid
2681-
///
2682-
/// ## Examples
2683-
/// ```dart
2684-
/// // Using default UUID validation
2685-
/// final validator = uuid();
2686-
/// print(validator('123e4567-e89b-12d3-a456-426614174000')); // null
2687-
/// print(validator('invalid-uuid')); // Returns error message
2688-
///
2689-
/// // Using custom error message
2690-
/// final customValidator = uuid(
2691-
/// uuidMsg: (input) => 'Invalid UUID format: $input',
2692-
/// );
2693-
///
2694-
/// // Using custom regex pattern
2695-
/// final customPatternValidator = uuid(
2696-
/// regex: RegExp(r'^[0-9]{8}-[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{12}$'),
2697-
/// );
2698-
/// ```
2699-
///
2700-
/// ## Caveats
2701-
/// - The default regex pattern accepts both uppercase and lowercase hexadecimal
2702-
/// digits (0-9, a-f, A-F)
2703-
/// - The validation only checks the format, not the actual UUID version or
2704-
/// variant compliance
2705-
/// {@endtemplate}
2706-
static Validator<String> uuid({
2707-
RegExp? regex,
2708-
String Function(String input)? uuidMsg,
2709-
}) =>
2710-
val.uuid(regex: regex, uuidMsg: uuidMsg);
2711-
27122662
/// {@template validator_contains}
27132663
/// Creates a validator function that checks if a string contains a specific
27142664
/// substring. The validation can be performed with or without case sensitivity.
@@ -3054,10 +3004,60 @@ final class Validators {
30543004
/// or phone number validation
30553005
/// {@endtemplate}
30563006
static Validator<String> match(
3057-
RegExp regex, {
3007+
RegExp regExp, {
30583008
String Function(String input)? matchMsg,
30593009
}) =>
3060-
val.match(regex, matchMsg: matchMsg);
3010+
val.match(regExp, matchMsg: matchMsg);
3011+
3012+
/// {@template validator_uuid}
3013+
/// A validator function that checks if a given string matches the UUID format.
3014+
///
3015+
/// Creates a validator that ensures the input string conforms to the standard
3016+
/// UUID (Universally Unique Identifier) format, consisting of 32 hexadecimal
3017+
/// digits displayed in 5 groups separated by hyphens (8-4-4-4-12).
3018+
///
3019+
/// ## Parameters
3020+
/// - `regex` (`RegExp?`): Optional custom regular expression pattern to override
3021+
/// the default UUID validation pattern. Useful for supporting different UUID
3022+
/// formats or adding additional constraints.
3023+
/// - `uuidMsg` (`String Function(String input)?`): Optional callback function that
3024+
/// generates a custom error message based on the invalid input. If not provided,
3025+
/// defaults to the standard form builder localization error text.
3026+
///
3027+
/// ## Returns
3028+
/// Returns a `Validator<String>` function that accepts a string input and returns:
3029+
/// - `null` if the input is valid
3030+
/// - An error message string if the input is invalid
3031+
///
3032+
/// ## Examples
3033+
/// ```dart
3034+
/// // Using default UUID validation
3035+
/// final validator = uuid();
3036+
/// print(validator('123e4567-e89b-12d3-a456-426614174000')); // null
3037+
/// print(validator('invalid-uuid')); // Returns error message
3038+
///
3039+
/// // Using custom error message
3040+
/// final customValidator = uuid(
3041+
/// uuidMsg: (input) => 'Invalid UUID format: $input',
3042+
/// );
3043+
///
3044+
/// // Using custom regex pattern
3045+
/// final customPatternValidator = uuid(
3046+
/// regex: RegExp(r'^[0-9]{8}-[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{12}$'),
3047+
/// );
3048+
/// ```
3049+
///
3050+
/// ## Caveats
3051+
/// - The default regex pattern accepts both uppercase and lowercase hexadecimal
3052+
/// digits (0-9, a-f, A-F)
3053+
/// - The validation only checks the format, not the actual UUID version or
3054+
/// variant compliance
3055+
/// {@endtemplate}
3056+
static Validator<String> uuid({
3057+
RegExp? regex,
3058+
String Function(String input)? uuidMsg,
3059+
}) =>
3060+
val.uuid(regex: regex, uuidMsg: uuidMsg);
30613061

30623062
// Collection validators
30633063
/// {@template validator_min_length}

lib/src/validators/string_validators.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ Validator<String> hasMinSpecialChars({
113113

114114
/// {@macro validator_match}
115115
Validator<String> match(
116-
RegExp regex, {
116+
RegExp regExp, {
117117
String Function(String input)? matchMsg,
118118
}) {
119119
return (String input) {
120-
return regex.hasMatch(input)
120+
return regExp.hasMatch(input)
121121
? null
122122
: matchMsg?.call(input) ??
123123
FormBuilderLocalizations.current.matchErrorText;

0 commit comments

Comments
 (0)