-
-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement the API modifications #129
Implement the API modifications #129
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## refactor_2024_11 #129 +/- ##
=====================================================
+ Coverage 86.44% 98.74% +12.29%
=====================================================
Files 109 111 +2
Lines 1483 1510 +27
=====================================================
+ Hits 1282 1491 +209
+ Misses 201 19 -182
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
@ArturAssisComp before implement all validators, I think that is better create a documentation on readme to be discuss about. |
@deandreamatias ok, I will do that. I implemented only the bool validators. I am using the version already implemented to make use of the tests, but today I will implement a prototype of the FormBuilderValidator using the new API to discuss the idea. Thanks for the feedback. |
I added a section at the end of the readme for the discussion. But it can also be discussed here. I made a video documentation about what I did too. |
I taken a look on this propouse. My thougths:
ValidatorBuilder.numeric(
errorText: 'La edad debe ser numérica.',
and: <BaseElementaryValidator<num, dynamic>>[
ValidatorBuilder.max(70),
]) could be simplify. A option is use |
I will investigate that with a prototype.
That is right. I forgot about the builder semantics that is specific to Flutter. I will think about another name.
I think that would be a good idea. I will investigate with prototypes too. |
implemented the new prototype
Tomorrow I will give a better comment on the new API prototype. But it is already implemented for the examples. Fell free to take a look at the new_api_prototype.dart if you want. Otherwise, wait for my comments. |
Conventions: I will call the API that is being used as "original api", the previous proposal as "initial prototype" and this prototype as "new prototype".
// All the use cases of the example/lib/main.dart were reproduced using the new prototype. They are in example/lib/app_refactoring_main.dart
// Some examples are
// Original api
FormBuilderValidators.compose(<FormFieldValidator<String>>[
/// Makes this field required
FormBuilderValidators.required(),
/// Ensures the value entered is numeric - with custom error message
FormBuilderValidators.numeric(
errorText: 'La edad debe ser numérica.',
),
/// Sets a maximum value of 70
FormBuilderValidators.max(70),
/// Include your own custom `FormFieldValidator` function, if you want
/// Ensures positive values only. We could also have used `FormBuilderValidators.min( 0)` instead
(String? val) {
if (val != null) {
final int? number = int.tryParse(val);
if (number == null) return null;
if (number < 0) return 'We cannot have a negative age';
}
return null;
}
]);
// New prototype
/// Makes this field required (req is an alias for isRequired)
req(
/// Ensures the value entered is numeric - with custom error message
isNum(
and([
/// Sets a maximum value of 70
max(70),
/// Include your own custom `FormFieldValidator` function, if you want
(num value) {
if (value < 0) return 'We cannot have a negative age';
return null;
},
]),
isNumMessage: 'La edad debe ser numérica.',
),
);
/// The previous example but using the compose method from the original api
FormBuilderValidators.compose(<FormFieldValidator<String>>[
req(isNum(max(70), isNumMessage: 'La edad debe ser numérica.')),
/// Include your own custom `FormFieldValidator` function, if you want
/// Ensures positive values only. We could also have used `FormBuilderValidators.min( 0)` instead
(String? val) {
if (val != null) {
final int? number = int.tryParse(val);
if (number == null) return null;
if (number < 0) return 'We cannot have a negative age';
}
return null;
}
])
// Composition with and
// Original api
FormBuilderValidators.compose(<FormFieldValidator<String>>[
FormBuilderValidators.required(),
FormBuilderValidators.numeric(),
FormBuilderValidators.min(0),
FormBuilderValidators.max(120),
]),
// New prototype
isRequired(isNum(and([min(0), max(120)])));
// Composition with or and and: The error message may be improved in the future with composition functions like: prefix, suffix, etc. There is already a composition validator called message that replaces the inner message with another one.
//(optional) Choose a value that is either a num in the set: (-10,5] U {7, 8} U (100, +inf) or an even integer
isOptional(or([
isNum(or([
and([gt(-10), ltE(5)]),
equal(7),
equal(8),
gt(100),
])),
isInt(
(int value) =>value % 2 == 0 ? null : 'The input must be even.',
),
]));
|
I also explored possibilities using operator overloading which would allow things like: req() * isNum() * (gt(10) * lt(15) + gtE(20)), using boolean logic notation as example. But it would become very complex to handle some edge cases and the error message composition, and the compiler would not help too much during the composition. It would be necessary to rely on some runtime check or to maintain the generality of each validator, maintaining the redundancy. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for delay, I wanted to see this with time and in detail.
I really like this new propouse. I think that this is the way to foward.
Co-authored-by: Matias de Andrea <[email protected]>
7f0c09b
into
flutter-form-builder-ecosystem:refactor_2024_11
Connection with issue(s)
Close #128
Connected to #119 #116
Solution description
Screenshots or Videos
To Do