Skip to content

Commit 484e639

Browse files
update readme
1 parent 8b235d1 commit 484e639

File tree

4 files changed

+51
-55
lines changed

4 files changed

+51
-55
lines changed

README-updated.md

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,13 @@ Generally, we build a validator composing those three types in the following way
104104

105105
#### Equality validators
106106

107-
TODO remove every verb `is` from the name of the validators.
108-
- `Validators.isEqual(value)`: Checks if the field contains an input that is equal to `value` (==).
109-
- `Validators.isNotEqual(value)`: Checks if the field contains an input that is not equal to `value` (!=).
107+
- `Validators.equal(value)`: Checks if the field contains an input that is equal to `value` (==).
108+
- `Validators.notEqual(value)`: Checks if the field contains an input that is not equal to `value` (!=).
110109

111110
#### Required validators
112111

113-
- `Validators.isRequired(next)`: Makes the field required by checking if it contains a non-null and non-empty value, passing it to the `next` validator as a not-nullable type.
114-
- `Validators.isOptional(next)`: Makes the field optional by passing it to the `next` validator if it contains a non-null and non-empty value. If the field is null or empty, null is returned.
112+
- `Validators.required(next)`: Makes the field required by checking if it contains a non-null and non-empty value, passing it to the `next` validator as a not-nullable type.
113+
- `Validators.optional(next)`: Makes the field optional by passing it to the `next` validator if it contains a non-null and non-empty value. If the field is null or empty, null is returned.
115114
- `Validators.validateWithDefault(defaultValue, next)`: Validates the field with `next` validator. If the input is null, it uses the `defaultValue` instead.
116115

117116
#### Transform validators
@@ -120,10 +119,9 @@ TODO remove every verb `is` from the name of the validators.
120119

121120
### Datetime validators
122121

123-
- `Validators.isAfter(reference)`: Checks if the field contains a `DateTime` that is after `reference`.
124-
- `Validators.isBefore(reference)`: Checks if the field contains a `DateTime` that is before `reference`.
125-
TODO replace isDateTimeBetween with betweenDateTime
126-
- `Validators.isDateTimeBetween(minReference, maxReference)`: Checks if the field contains a `DateTime` that is after `minReference` and before `maxReference`.
122+
- `Validators.after(reference)`: Checks if the field contains a `DateTime` that is after `reference`.
123+
- `Validators.before(reference)`: Checks if the field contains a `DateTime` that is before `reference`.
124+
- `Validators.betweenDateTime(minReference, maxReference)`: Checks if the field contains a `DateTime` that is after `minReference` and before `maxReference`.
127125
- TODO `FormBuilderValidators.date()` - requires the field's value to be a valid date string.
128126
- TODO `FormBuilderValidators.time()` - requires the field's value to be a valid time string.
129127
- TODO `FormBuilderValidators.timeZone()` - requires the field's value to be a valid time zone.
@@ -147,7 +145,7 @@ TODO replace isDateTimeBetween with betweenDateTime
147145
### Generic Type Validators
148146
Validators that check a generic type user input.
149147

150-
- `Validators.isInList(values)`: Checks if the field contains a value that is in the list `values`.
148+
- `Validators.inList(values)`: Checks if the field contains a value that is in the list `values`.
151149
- `Validators.isTrue()`: Checks if the field contains a boolean or a parsable `String` of the `true` value.
152150
- `Validators.isFalse()`: Checks if the field contains a boolean or a parsable `String` of the `false` value.
153151

@@ -209,12 +207,12 @@ Validators that check a generic type user input.
209207
- TODO `FormBuilderValidators.singleLine()` - requires the field's string to be a single line of text.
210208

211209
### Type Validators
212-
- `Validators.isString(next)`: Checks if the field contains a valid `String` and passes the input as `String` to the `next` validator.
213-
- `Validators.isInt(next)`: Checks if the field contains a valid `int` or parsable `String` to `int` and passes the input as `int` to the `next` validator.
214-
- `Validators.isDouble(next)`: Checks if the field contains a valid `double` or parsable `String` to `double` and passes the input as `double` to the `next` validator.
215-
- `Validators.isNum(next)`: Checks if the field contains a valid `num` or parsable `String` to `num` and passes the input as `num` to the `next` validator.
216-
- `Validators.isBool(next)`: Checks if the field contains a valid `bool` or parsable `String` to `bool` and passes the input as `bool` to the `next` validator.
217-
- `Validators.isDateTime(next)`: Checks if the field contains a valid `DateTime` or parsable `String` to `DateTime` and passes the input as `DateTime` to the `next` validator.
210+
- `Validators.string(next)`: Checks if the field contains a valid `String` and passes the input as `String` to the `next` validator.
211+
- `Validators.int(next)`: Checks if the field contains a valid `int` or parsable `String` to `int` and passes the input as `int` to the `next` validator.
212+
- `Validators.double(next)`: Checks if the field contains a valid `double` or parsable `String` to `double` and passes the input as `double` to the `next` validator.
213+
- `Validators.num(next)`: Checks if the field contains a valid `num` or parsable `String` to `num` and passes the input as `num` to the `next` validator.
214+
- `Validators.bool(next)`: Checks if the field contains a valid `bool` or parsable `String` to `bool` and passes the input as `bool` to the `next` validator.
215+
- `Validators.dateTime(next)`: Checks if the field contains a valid `DateTime` or parsable `String` to `DateTime` and passes the input as `DateTime` to the `next` validator.
218216

219217
### User Information validators
220218

@@ -316,7 +314,7 @@ return MaterialApp(
316314
TextFormField(
317315
decoration: InputDecoration(labelText: 'Name'),
318316
autovalidateMode: AutovalidateMode.always,
319-
validator: Validators.isRequired(),
317+
validator: Validators.required(),
320318
),
321319
```
322320

@@ -338,25 +336,23 @@ TextFormField(
338336
decoration: InputDecoration(labelText: 'Age'),
339337
keyboardType: TextInputType.number,
340338
autovalidateMode: AutovalidateMode.always,
341-
validator: FormBuilderValidators.compose([
342-
/// Makes this field required
343-
FormBuilderValidators.required(),
344-
345-
/// Ensures the value entered is numeric - with a custom error message
346-
FormBuilderValidators.numeric(errorText: 'La edad debe ser numérica.'),
347-
348-
/// Sets a maximum value of 70
349-
FormBuilderValidators.max(70),
339+
validator: Validators.required(
340+
Validators.and(<Validator<String>>[
341+
Validators.num(Validators.lessThan(70), (_) => 'La edad debe ser numérica.'),
350342
351343
/// Include your own custom `FormFieldValidator` function, if you want
352-
/// Ensures positive values only. We could also have used `FormBuilderValidators.min(0)` instead
353-
(val) {
354-
final number = int.tryParse(val);
355-
if (number == null) return null;
356-
if (number < 0) return 'We cannot have a negative age';
344+
/// Ensures positive values only. We could also have used `Validators.greaterThanOrEqualTo(0)` instead
345+
(String? val) {
346+
if (val != null) {
347+
final int? number = int.tryParse(val);
348+
// todo bug here: if it is not int, it accepts negative
349+
// numbers
350+
if (number == null) return null;
351+
if (number < 0) return 'We cannot have a negative age';
352+
}
357353
return null;
358-
},
359-
]),
354+
}
355+
]))
360356
),
361357
```
362358

@@ -422,14 +418,14 @@ String? Function(int) isEven(){
422418
};
423419
}
424420
425-
String? Function(int?) isRequired(String? Function(int) next){
421+
String? Function(int?) required(String? Function(int) next){
426422
return (int? userInput) {
427423
return (userInput != null) ? next(userInput):'This field is required';
428424
};
429425
}
430426
431-
// Important: isEven() does not return a FormFieldValidator, but the composition isRequired(isEven()), does.
432-
final validator = isRequired(isEven());
427+
// Important: isEven() does not return a FormFieldValidator, but the composition required(isEven()), does.
428+
final validator = required(isEven());
433429
```
434430

435431
By introducing this level of indirection, we achieve:

lib/src/form_builder_validators.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3409,7 +3409,7 @@ final class Validators {
34093409
}) =>
34103410
val.before(reference, beforeMsg: beforeMsg, inclusive: inclusive);
34113411

3412-
/// {@template validator_date_time_between}
3412+
/// {@template validator_between_date_time}
34133413
/// Creates a [DateTime] validator that checks if an input date falls within a specified
34143414
/// range defined by `minReference` and `maxReference`.
34153415
///
@@ -3422,7 +3422,7 @@ final class Validators {
34223422
/// Input dates must occur after this date (or equal to it if `minInclusive` is true).
34233423
/// - `maxReference` (`DateTime`): The upper bound of the acceptable date range.
34243424
/// Input dates must occur before this date (or equal to it if `maxInclusive` is true).
3425-
/// - `dateTimeBetweenMsg` (`String Function(DateTime, DateTime, DateTime)?`): Optional
3425+
/// - `betweenDateTimeMsg` (`String Function(DateTime, DateTime, DateTime)?`): Optional
34263426
/// custom error message generator. When provided, it receives the input date and both
34273427
/// reference dates to construct a context-aware error message.
34283428
/// - `minInclusive` (`bool`): When set to `true`, allows the input date to exactly match
@@ -3443,39 +3443,39 @@ final class Validators {
34433443
/// ## Examples
34443444
/// ```dart
34453445
/// // Basic usage requiring date between 2023 and 2025
3446-
/// final validator = Validators.dateTimeBetween(
3446+
/// final validator = Validators.betweenDateTime(
34473447
/// DateTime(2023),
34483448
/// DateTime(2025),
34493449
/// );
34503450
///
34513451
/// // Inclusive validation allowing exact matches
3452-
/// final inclusiveValidator = Validators.dateTimeBetween(
3452+
/// final inclusiveValidator = Validators.betweenDateTime(
34533453
/// DateTime(2023),
34543454
/// DateTime(2025),
34553455
/// minInclusive: true,
34563456
/// maxInclusive: true,
34573457
/// );
34583458
///
34593459
/// // Custom error message
3460-
/// final customValidator = Validators.dateTimeBetween(
3460+
/// final customValidator = Validators.betweenDateTime(
34613461
/// DateTime(2023),
34623462
/// DateTime(2025),
3463-
/// isDateTimeBetweenMsg: (_, min, max) =>
3463+
/// betweenDateTimeMsg: (_, min, max) =>
34643464
/// 'Please select a date between ${min.toString()} and ${max.toString()}',
34653465
/// );
34663466
/// ```
34673467
/// {@endtemplate}
3468-
static Validator<DateTime> dateTimeBetween(
3468+
static Validator<DateTime> betweenDateTime(
34693469
DateTime minReference,
34703470
DateTime maxReference, {
34713471
String Function(
34723472
DateTime input, DateTime minReference, DateTime maxReference)?
3473-
dateTimeBetweenMsg,
3473+
betweenDateTimeMsg,
34743474
c.bool leftInclusive = false,
34753475
c.bool rightInclusive = false,
34763476
}) =>
3477-
val.dateTimeBetween(minReference, maxReference,
3478-
dateTimeBetweenMsg: dateTimeBetweenMsg,
3477+
val.betweenDateTime(minReference, maxReference,
3478+
betweenDateTimeMsg: betweenDateTimeMsg,
34793479
minInclusive: leftInclusive,
34803480
maxInclusive: rightInclusive);
34813481

lib/src/validators/datetime_validators.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ Validator<DateTime> before(
3333
};
3434
}
3535

36-
/// {@macro validator_date_time_between}
37-
Validator<DateTime> dateTimeBetween(
36+
/// {@macro validator_between_date_time}
37+
Validator<DateTime> betweenDateTime(
3838
DateTime minReference,
3939
DateTime maxReference, {
4040
String Function(DateTime input, DateTime minReference, DateTime maxReference)?
41-
dateTimeBetweenMsg,
41+
betweenDateTimeMsg,
4242
bool minInclusive = false,
4343
bool maxInclusive = false,
4444
}) {
@@ -52,7 +52,7 @@ Validator<DateTime> dateTimeBetween(
5252
(input.isAfter(minReference) ||
5353
(minInclusive ? input.isAtSameMomentAs(minReference) : false))
5454
? null
55-
: dateTimeBetweenMsg?.call(input, minReference, maxReference) ??
55+
: betweenDateTimeMsg?.call(input, minReference, maxReference) ??
5656
FormBuilderLocalizations.current.dateMustBeBetweenErrorText(
5757
minReference.toLocal(), maxReference.toLocal());
5858
};

test/src/validators/datetime_validators/datetime_between_validator_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void main() {
2525
final DateTime beforeLeft1Micro =
2626
leftReference.subtract(const Duration(microseconds: 1));
2727
final Validator<DateTime> v =
28-
dateTimeBetween(leftReference, rightReference);
28+
betweenDateTime(leftReference, rightReference);
2929
final String errorMsg = FormBuilderLocalizations.current
3030
.dateMustBeBetweenErrorText(leftReference, rightReference);
3131

@@ -91,7 +91,7 @@ void main() {
9191
final DateTime before1Sec =
9292
leftReference.subtract(const Duration(seconds: 1));
9393
final Validator<DateTime> v =
94-
dateTimeBetween(leftReference, rightReference, minInclusive: true);
94+
betweenDateTime(leftReference, rightReference, minInclusive: true);
9595
final String errorMsg = FormBuilderLocalizations.current
9696
.dateMustBeBetweenErrorText(leftReference, rightReference);
9797

@@ -145,9 +145,9 @@ void main() {
145145
const String errorMsg = 'error msg';
146146
final DateTime leftReference = DateTime(2);
147147
final DateTime rightReference = DateTime(5);
148-
final Validator<DateTime> v = dateTimeBetween(
148+
final Validator<DateTime> v = betweenDateTime(
149149
leftReference, rightReference,
150-
dateTimeBetweenMsg: (_, __, ___) => errorMsg);
150+
betweenDateTimeMsg: (_, __, ___) => errorMsg);
151151

152152
expect(
153153
v(rightReference.copyWith()),
@@ -173,7 +173,7 @@ void main() {
173173
'Should throw AssertionError when the right reference is not after left reference',
174174
() {
175175
expect(
176-
() => dateTimeBetween(
176+
() => betweenDateTime(
177177
DateTime(1990, 12, 23, 20), DateTime(1990, 12, 22, 20)),
178178
throwsAssertionError);
179179
});

0 commit comments

Comments
 (0)