Skip to content

Integrate flutter_validators for form field validation#477

Merged
divyanshub024 merged 5 commits into
devfrom
dv/flutter-validators
May 16, 2026
Merged

Integrate flutter_validators for form field validation#477
divyanshub024 merged 5 commits into
devfrom
dv/flutter-validators

Conversation

@divyanshub024
Copy link
Copy Markdown
Member

@divyanshub024 divyanshub024 commented May 16, 2026

Summary

  • Replace the homegrown InputValidationType regex validators with the flutter_validators package, exposing 38 validators (isEmail, isURL, isUUID, isStrongPassword, isLength, etc.) to StacTextFormField validatorRules.
  • Add an optional options map to StacFormFieldValidator so parameterized rules can receive arguments (e.g. isLength min/max, isStrongPassword config).
  • Support raw-regex validation via the matches rule — options.pattern is compiled into a RegExp.
  • compare (confirm-password style cross-field check) is handled in the parser via options.fieldId, resolving against the form scope. This also fixes a latent bug where compare never received a value to compare against.
  • Fix: a failing validator with a null message was silently treated as valid (Flutter reads null as "no error"); it now falls back to Invalid input.

Notes

  • Full replacement — the old custom rules (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 explicit matches rule.
  • The gallery form_example.json username field used a raw-regex rule and was updated to isAlphanumeric + isLength; the password field gained an isStrongPassword rule.
  • Known gap (not addressed here): flutter_validators fails on empty strings, so any field with validatorRules is effectively required — there is no "optional field" concept yet.

Test plan

  • flutter test in packages/stac passes
  • flutter analyze clean across stac and stac_core
  • Run stac_gallery, open the "Sign in Form" screen: invalid input shows the custom message, valid input clears errors and submits

Summary by CodeRabbit

  • New Features

    • Parameterized form validation rules with per-rule options and field-to-field comparison
    • Expanded validator set (e.g., strength and length checks) and stricter handling of empty input
  • Documentation

    • Added comprehensive "Validator Rules" section with formats, examples, anchoring/matching and behavior notes
  • Chores

    • Introduced centralized validator implementation to support the extended rule set

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 16, 2026

Note

Reviews paused

It 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 reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ea6ed0cd-67a6-4af3-99de-7894de62dae1

📥 Commits

Reviewing files that changed from the base of the PR and between 5f8158b and 4cfb306.

📒 Files selected for processing (3)
  • examples/stac_gallery/assets/json/form_example.json
  • packages/stac/lib/src/parsers/widgets/stac_text_form_field/stac_text_form_field_parser.dart
  • packages/stac/lib/src/utils/input_validations.dart
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/stac/lib/src/parsers/widgets/stac_text_form_field/stac_text_form_field_parser.dart
  • packages/stac/lib/src/utils/input_validations.dart

📝 Walkthrough

Walkthrough

Adds parameterized form-field validators: StacFormFieldValidator gains an options map; InputValidators delegates rule names to flutter_validators with option coercion; parser iterates rules, supports a compare rule via fieldId, and returns per-rule messages; docs and example JSON updated (username and password validators).

Changes

Parameterized Form Field Validation

Layer / File(s) Summary
Validator model contract and serialization
packages/stac_core/lib/foundation/forms/stac_form_field_validator/stac_form_field_validator.dart, packages/stac_core/lib/foundation/forms/stac_form_field_validator/stac_form_field_validator.g.dart, packages/stac/pubspec.yaml
StacFormFieldValidator constructor now accepts optional options; new options field added; docs expanded; JSON (de)serialization updated to include options; flutter_validators ^1.2.0 added to dependencies.
Validation engine and rule map
packages/stac/lib/src/utils/input_validations.dart
Introduced InputValidators with a private rule→function map delegating Stac rule strings to flutter_validators functions; validate invokes mapped validators with options (returns true for unknown rules); hasRule added; helper converters coerce option types.
Parser validation dispatch with compare rule
packages/stac/lib/src/parsers/widgets/stac_text_form_field/stac_text_form_field_parser.dart
_validate now early-returns null for null value or empty rules, iterates model.validatorRules, handles compare via options['fieldId'] to compare fields, delegates other rules to InputValidators.validate, and returns validator.message or default on failure.
Documentation and configuration examples
docs/widgets/text_form_field.mdx, examples/stac_gallery/assets/json/form_example.json
Added "Validator Rules" doc section (rule schema, supported validators, parameter options, matches anchoring, compare guidance, example JSON) and updated example JSON: username uses isAlphanumeric + isLength; password adds isLength min:1 with "Password is required" message.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

A rabbit hops through validator gates,
Options tucked in pockets, checking traits,
Engines call validators, fields compare,
JSON examples flutter in the air,
Passwords and lengths — validated with care. 🐰✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: integrating the flutter_validators package for form field validation, which is the core objective across all modified files.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch dv/flutter-validators

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.
@divyanshub024 divyanshub024 force-pushed the dv/flutter-validators branch from 69c636f to 7c27f3b Compare May 16, 2026 13:39
@divyanshub024 divyanshub024 marked this pull request as ready for review May 16, 2026 13:39
@divyanshub024
Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 16, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 755079a and 7c27f3b.

⛔ Files ignored due to path filters (1)
  • examples/stac_gallery/pubspec.lock is excluded by !**/*.lock
📒 Files selected for processing (7)
  • docs/widgets/text_form_field.mdx
  • examples/stac_gallery/assets/json/form_example.json
  • packages/stac/lib/src/parsers/widgets/stac_text_form_field/stac_text_form_field_parser.dart
  • packages/stac/lib/src/utils/input_validations.dart
  • packages/stac/pubspec.yaml
  • packages/stac_core/lib/foundation/forms/stac_form_field_validator/stac_form_field_validator.dart
  • packages/stac_core/lib/foundation/forms/stac_form_field_validator/stac_form_field_validator.g.dart

Comment thread docs/widgets/text_form_field.mdx
Comment thread examples/stac_gallery/assets/json/form_example.json
Comment thread packages/stac/lib/src/utils/input_validations.dart Outdated
@divyanshub024 divyanshub024 force-pushed the dv/flutter-validators branch from 7c27f3b to 3defaca Compare May 16, 2026 13:46
@divyanshub024 divyanshub024 force-pushed the dv/flutter-validators branch from 3defaca to 9389943 Compare May 16, 2026 13:47
@divyanshub024 divyanshub024 merged commit 42c96e8 into dev May 16, 2026
6 checks passed
@divyanshub024 divyanshub024 deleted the dv/flutter-validators branch May 16, 2026 18:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants