-
-
Notifications
You must be signed in to change notification settings - Fork 104
Integrate flutter_validators for form field validation #477
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
37653a7
Integrate flutter_validators for form field validation
divyanshub024 9389943
Document validatorRules in TextFormField docs
divyanshub024 24095ac
Update example lockfiles for flutter_validators dependency
divyanshub024 5f8158b
Document empty-string behavior of validatorRules
divyanshub024 4cfb306
Harden validatorRules: fail closed and fix matches/example
divyanshub024 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,105 @@ | ||
| enum InputValidationType { | ||
| isEmail, | ||
| isName, | ||
| isPassword, | ||
| isNotEmpty, | ||
| compare, | ||
| general; | ||
|
|
||
| RegExp get _emailRegExp => RegExp( | ||
| r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$', | ||
| ); | ||
| RegExp get _nameRegExp => RegExp(r"^[A-Za-z .`'/-]{2,32}$"); | ||
| RegExp get _passwordRegExp => RegExp( | ||
| r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@#$!%*?&])[A-Za-z\d@#$%^&£*\-_+=[\]{}|\\:,?\/`~""()<>;!]*$', | ||
| ); | ||
| RegExp get _isNotEmptyRegExp => RegExp("^[0-9a-zA-Z .`'/-]{1,50}\$"); | ||
|
|
||
| bool validate(String value, String rule, {String? compareValue}) { | ||
| switch (this) { | ||
| case InputValidationType.isEmail: | ||
| return _emailRegExp.hasMatch(value); | ||
|
|
||
| case InputValidationType.isName: | ||
| return _nameRegExp.hasMatch(value); | ||
|
|
||
| case InputValidationType.isPassword: | ||
| return _passwordRegExp.hasMatch(value); | ||
|
|
||
| case InputValidationType.isNotEmpty: | ||
| return _isNotEmptyRegExp.hasMatch(value); | ||
|
|
||
| case InputValidationType.compare: | ||
| return value == compareValue; | ||
|
|
||
| default: | ||
| return RegExp(rule).hasMatch(value); | ||
| } | ||
| import 'package:flutter_validators/flutter_validators.dart'; | ||
|
|
||
| typedef _RuleValidator = | ||
| bool Function(String value, Map<String, dynamic>? options); | ||
|
|
||
| /// Maps Stac `validatorRules` rule strings to `flutter_validators` functions. | ||
| /// | ||
| /// Every rule delegates to the `flutter_validators` package — Stac keeps no | ||
| /// homegrown validation logic. Parameterized validators read their arguments | ||
| /// from the rule's `options` map. | ||
| class InputValidators { | ||
| InputValidators._(); | ||
|
|
||
| static final Map<String, _RuleValidator> _rules = { | ||
| 'isAlpha': (v, o) => isAlpha(v), | ||
| 'isAlphanumeric': (v, o) => isAlphanumeric(v), | ||
| 'isAscii': (v, o) => isAscii(v), | ||
| 'isBase32': (v, o) => isBase32(v), | ||
| 'isBase58': (v, o) => isBase58(v), | ||
| 'isBase64': (v, o) => isBase64(v, urlSafe: o?['urlSafe'] == true), | ||
| 'isBoolean': (v, o) => isBoolean(v), | ||
| 'isCreditCard': (v, o) => isCreditCard(v), | ||
| 'isDate': (v, o) => isDate(v), | ||
| 'isDecimal': (v, o) => isDecimal(v), | ||
| 'isEmail': (v, o) => isEmail(v), | ||
| 'isFQDN': (v, o) => isFQDN(v), | ||
| 'isFloat': (v, o) => | ||
| isFloat(v, min: _toDouble(o?['min']), max: _toDouble(o?['max'])), | ||
| 'isHexColor': (v, o) => isHexColor(v), | ||
| 'isHexadecimal': (v, o) => isHexadecimal(v), | ||
| 'isInt': (v, o) => isInt(v), | ||
| 'isIP': (v, o) => isIP(v, _toInt(o?['version'])), | ||
| 'isJson': (v, o) => isJson(v), | ||
| 'isJWT': (v, o) => isJWT(v), | ||
| 'isLatLong': (v, o) => isLatLong(v), | ||
| 'isLowercase': (v, o) => isLowercase(v), | ||
| 'isMACAddress': (v, o) => isMACAddress(v), | ||
| 'isMD5': (v, o) => isMD5(v), | ||
| 'isMongoId': (v, o) => isMongoId(v), | ||
| 'isNumeric': (v, o) => isNumeric(v), | ||
| 'isOctal': (v, o) => isOctal(v), | ||
| 'isPhone': (v, o) => isPhone(v), | ||
| 'isPort': (v, o) => isPort(v), | ||
| 'isSemVer': (v, o) => isSemVer(v), | ||
| 'isSlug': (v, o) => isSlug(v), | ||
| 'isUppercase': (v, o) => isUppercase(v), | ||
| 'isURL': (v, o) => isURL(v), | ||
| 'isUUID': (v, o) => isUUID(v), | ||
| 'isByteLength': (v, o) => | ||
| isByteLength(v, _toInt(o?['min']) ?? 0, _toInt(o?['max'])), | ||
| 'isLength': (v, o) => | ||
| isLength(v, _toInt(o?['min']) ?? 0, _toInt(o?['max'])), | ||
| 'isIn': (v, o) => isIn(v, _toStringList(o?['values'])), | ||
| 'contains': (v, o) => contains( | ||
| v, | ||
| o?['seed']?.toString() ?? '', | ||
| ignoreCase: o?['ignoreCase'] == true, | ||
| minOccurrences: _toInt(o?['minOccurrences']) ?? 1, | ||
| ), | ||
| 'equals': (v, o) => equals(v, o?['comparison']?.toString() ?? ''), | ||
| 'matches': (v, o) { | ||
| final pattern = o?['pattern']?.toString(); | ||
| if (pattern == null || pattern.isEmpty) return false; | ||
| try { | ||
| return matches(v, RegExp(pattern)); | ||
| } catch (_) { | ||
| return false; | ||
| } | ||
| }, | ||
| 'isStrongPassword': (v, o) => isStrongPassword( | ||
| v, | ||
| minLength: _toInt(o?['minLength']) ?? 8, | ||
| minLowercase: _toInt(o?['minLowercase']) ?? 1, | ||
| minUppercase: _toInt(o?['minUppercase']) ?? 1, | ||
| minNumbers: _toInt(o?['minNumbers']) ?? 1, | ||
| minSymbols: _toInt(o?['minSymbols']) ?? 1, | ||
| ), | ||
| }; | ||
|
|
||
| /// Validates [value] against [rule], reading any arguments from [options]. | ||
| /// | ||
| /// Unknown rules pass (return `true`) — the rule set is owned upstream by | ||
| /// the `flutter_validators` package. | ||
| static bool validate( | ||
| String rule, | ||
| String value, { | ||
| Map<String, dynamic>? options, | ||
| }) { | ||
| final validator = _rules[rule]; | ||
| if (validator == null) return true; | ||
| return validator(value, options); | ||
| } | ||
|
|
||
| /// Whether [rule] is a known `flutter_validators` rule. | ||
| static bool hasRule(String rule) => _rules.containsKey(rule); | ||
|
|
||
| static int? _toInt(Object? value) => | ||
| value is int ? value : (value is num ? value.toInt() : null); | ||
|
|
||
| static double? _toDouble(Object? value) => | ||
| value is num ? value.toDouble() : null; | ||
|
|
||
| static Iterable<String> _toStringList(Object? value) => | ||
| value is Iterable ? value.map((e) => e.toString()) : const <String>[]; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.