Integrate flutter_validators for form field validation#477
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughAdds parameterized form-field validators: ChangesParameterized Form Field Validation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Replace the homegrown InputValidationType regex validators with the flutter_validators package, exposing 38 validators to StacTextFormField validatorRules. Add an options map to StacFormFieldValidator for parameterized rules (isLength, isStrongPassword, matches, etc.) and support raw-regex validation via the matches rule.
69c636f to
7c27f3b
Compare
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 5
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@docs/widgets/text_form_field.mdx`:
- Around line 116-125: The docs for validatorRules and StacFormFieldValidator
must explicitly note that flutter_validators treats empty strings as invalid, so
any field using validatorRules becomes effectively required unless callers add
handling; update the text near the validatorRules description (and the duplicate
mention around line 143) to add a short caveat explaining this empty-string
behavior and recommend either adding a custom validator to allow empty values or
pre-checking for blank input before applying flutter_validators rules.
In `@examples/stac_gallery/assets/json/form_example.json`:
- Around line 61-66: The form example's validatorRules uses "isStrongPassword",
which causes validateForm to reject the provided sample credential ("password":
"0lelplR"); remove or replace that rule so the sign-in flow in examples works.
Update the JSON under "validatorRules" either by deleting the object with
"rule": "isStrongPassword" or swapping it for a permissive rule (e.g.,
"required") and adjust the "message" accordingly so validateForm no longer
blocks login in the example.
In
`@packages/stac/lib/src/parsers/widgets/stac_text_form_field/stac_text_form_field_parser.dart`:
- Around line 137-156: The current try/catch around the validation logic (the
block handling validator.rule, the compare branch that reads
widget.formScope?.formData, and the call to InputValidators.validate) swallows
exceptions by only calling Log.e(e), which lets invalid inputs pass; change the
catch to fail closed: after logging the error with Log.e(e) return
validator.message ?? 'Invalid input' (or rethrow) so any exception in
validator.rule or InputValidators.validate results in a validation failure
instead of silently succeeding. Ensure this change is made in the same try/catch
that surrounds the compare branch and InputValidators.validate call.
- Around line 132-134: The validator runs on empty strings (TextFormField gives
''), making optional fields fail; in stac_text_form_field_parser.dart update the
early-return that currently checks value == null ||
!(model.validatorRules?.isNotEmpty ?? false) to also treat empty/blank strings
as "no value" (e.g. if value is String and value.trim().isEmpty) and return
null, but if you support comparison rules keep them by only skipping non-compare
validatorRules on empty input; use model.validatorRules to detect compare-type
rules and only run those when value is blank.
In `@packages/stac/lib/src/utils/input_validations.dart`:
- Line 61: The current 'matches' validator builds RegExp('') when o?['pattern']
is missing, which always passes; change the 'matches' entry so it first verifies
that o?['pattern'] is a non-empty string and attempt to construct a RegExp
inside a try/catch (or catch FormatException); if the pattern is missing/empty
or constructing the RegExp fails, return false (i.e., validation fails) instead
of creating an empty RegExp, otherwise run RegExp.hasMatch on the value as
before.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 556d156f-8c14-4d72-8901-e4af88b64b79
⛔ Files ignored due to path filters (1)
examples/stac_gallery/pubspec.lockis excluded by!**/*.lock
📒 Files selected for processing (7)
docs/widgets/text_form_field.mdxexamples/stac_gallery/assets/json/form_example.jsonpackages/stac/lib/src/parsers/widgets/stac_text_form_field/stac_text_form_field_parser.dartpackages/stac/lib/src/utils/input_validations.dartpackages/stac/pubspec.yamlpackages/stac_core/lib/foundation/forms/stac_form_field_validator/stac_form_field_validator.dartpackages/stac_core/lib/foundation/forms/stac_form_field_validator/stac_form_field_validator.g.dart
7c27f3b to
3defaca
Compare
3defaca to
9389943
Compare
Summary
InputValidationTyperegex validators with theflutter_validatorspackage, exposing 38 validators (isEmail,isURL,isUUID,isStrongPassword,isLength, etc.) toStacTextFormFieldvalidatorRules.optionsmap toStacFormFieldValidatorso parameterized rules can receive arguments (e.g.isLengthmin/max,isStrongPasswordconfig).matchesrule —options.patternis compiled into aRegExp.compare(confirm-password style cross-field check) is handled in the parser viaoptions.fieldId, resolving against the form scope. This also fixes a latent bug wherecomparenever received a value to compare against.nullmessagewas silently treated as valid (Flutter readsnullas "no error"); it now falls back toInvalid input.Notes
isName,isPassword,isNotEmpty) and the implicit raw-regex fallback are removed. Unknown rules now pass as a no-op; raw regex must go through the explicitmatchesrule.form_example.jsonusername field used a raw-regex rule and was updated toisAlphanumeric+isLength; the password field gained anisStrongPasswordrule.flutter_validatorsfails on empty strings, so any field withvalidatorRulesis effectively required — there is no "optional field" concept yet.Test plan
flutter testinpackages/stacpassesflutter analyzeclean acrossstacandstac_corestac_gallery, open the "Sign in Form" screen: invalid input shows the custom message, valid input clears errors and submitsSummary by CodeRabbit
New Features
Documentation
Chores